# Local Model Pitfalls

> Diagnose and fix silent context truncation, empty thinking-model outputs, and Gemini API parameter failures.

Local Model Pitfalls is a free AI agent skill on Agensi by Phil Marcus, in LLM Ops. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [Phil Marcus](https://www.agensi.io/creators/phil-marcus)
- Category: [LLM Ops](https://www.agensi.io/skills/llm-ops)
- Uses: 2
- Version: 1.0
- Last updated: 2026-10-19
- Security: scan passed (2026-10-19); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/local-model-pitfalls
- Tags: llm-ops, ollama, gemini, debugging, python

## About this skill

Local and hosted models often fail silently when integrated into agent harnesses. A correct implementation can appear broken due to hidden context truncation, budget-exhausted thinking blocks, or deprecated API parameters. This skill provides the diagnostic logic and code to identify and fix these three specific points of failure.

### What it does
- **Dynamic Context Sizing** calculates the required `num_ctx` for Ollama based on prompt character count and model limits to prevent front-end truncation.
- **Thinking Model Guards** identify when models exhaust their output budget on reasoning and return empty strings, preventing data loss in structured tasks.
- **Gemini Parameter Enforcement** manages the transition between legacy sampling parameters and new thinking levels to avoid 400 errors.
- **Automated Probing** includes a script to verify context truncation on your local hardware by hiding and retrieving tokens in padded prompts.
- **JSON Sanitization** provides a parser that strips leaked reasoning blocks before processing structured data.

### How it works
1. Use `ollama_ctx.py` to calculate the correct context window before every call to ensure the system prompt isn't dropped.
2. Run the `probe` command to confirm if your current local setup is silently truncating data.
3. Apply the thinking-guard logic to structured tasks like classification or compression to handle empty string failures.
4. Reference the version-specific policy table for Gemini to gate temperature and thinking parameters correctly.

### Frameworks & tools
Python 3.9+ standard library. Works with local Ollama instances via the REST API and Gemini 3.x hosted models.

### Why this beats prompting it yourself
Prompting cannot fix engine-level truncation or API-level parameter rejections. This skill provides the infrastructure-level logic required to ensure the model actually receives the prompt you sent.

### Use cases
- Preventing "memory loss" in long-context local agent loops.
- Fixing empty JSON responses from models like DeepSeek-R1 or Qwen.
- Migrating Gemini integrations to 3.x models without 400 errors on sampling or thinking parameters.
- Debugging why a local 12B model ignores its system instructions.

### Known limitations
The probe script and context sizer require a reachable local Ollama API. Context sizing uses a character-to-token heuristic which may vary slightly by tokenizer.

## Requires

- Python 3.9+
- Optional: Ollama

Ollama is for the probe and show commands.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Local Model Pitfalls skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### What specific 'pitfalls' does this skill help me avoid?

This skill addresses "silent failures" where a model appears to work but provides poor results due to hidden context truncation, budget-exhausted thinking blocks, or incompatible API parameters. It provides the diagnostic logic to detect these errors and the code to fix them.

### What exactly is included in the download?

The skill includes Python-based diagnostic scripts, a context-sizing utility for Ollama, a Gemini parameter policy table, and sanitization logic to handle thinking-model outputs. You will receive the full source code and documentation for these modules.

### Can I use this with my existing agent framework?

Yes. It is Python 3.9+ standard library and works with any agent harness that calls Ollama over its REST API or Gemini 3.x hosted models; the sizing function takes a character count and returns a num_ctx, so it drops into any client. Tested in Claude Code and OpenClaw; the OpenClaw reference has the exact Modelfile and openclaw.json fix.

### How does the skill detect if my local model is silently truncating context?

Two ways. The tell in production is prompt_eval_count in the Ollama response: if it is far below the prompt estimate (characters divided by 3 to 4), the front of the prompt was dropped. The probe command demonstrates it live on your machine: it hides a secret word at the start of a padded prompt and asks for it at the end, once at the default context and once with a sized num_ctx, and prints both answers and both prompt_eval_counts.

### Why can't I just fix these issues by improving my system prompt?

Prompting cannot help when the model never sees the prompt. Ollama drops the oldest tokens past its default 4096 context without an error, a thinking model that spends its whole output budget reasoning returns nothing to parse, and a Gemini 3.x model ignores or rejects the sampling parameters you send. These are engine and API behaviours, so the fixes are in the calling code: size num_ctx per call, turn thinking off for short structured outputs and treat empty output as failure, and gate parameters by model version.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
