Fix raw XML tool calls from Ollama models in OpenCode
This article is about changing the default context size of a local model served by Ollama. The goal: give your model a large enough context window so it stops truncating your prompts and can format tool calls correctly.
When you see raw text like <function=explore> or XML tags printed in OpenCode, the model is outputting plain text instead of returning a structured JSON tool call.
This happens with Ollama and Qwen models because Ollama defaults to a 2,048 or 4,096 token context window (num_ctx). OpenCode sends large system prompts and tool schemas; when the context window fills up, Ollama silently truncates the prompt. As a result, Qwen loses the system instructions for tool formatting and falls back to printing raw function text.
Step 1: create a model variant with an extended context
Create a custom model variant in Ollama that expands the context length to 16,384 or 32,768 tokens.
- Create a file named
Modelfilein your terminal:
FROM qwen3-coder
PARAMETER num_ctx 32768
PARAMETER temperature 0.2
Replace qwen3-coder with your exact base model name if different, such as qwen2.5-coder:7b. For a broader overview of how Ollama models and Modelfiles are configured, see our complete Ollama guide.
- Build the updated model variant:
ollama create qwen3-coder-32k -f Modelfile
Run `ollama list` to see your installed models and use the exact name of the Qwen model you already pulled.
What does temperature 0.2 do?
In Ollama, setting PARAMETER temperature 0.2 in a Modelfile controls the randomness and creativity of the model's responses.
Temperature operates on a scale typically ranging from 0.0 to 1.0 (though it can go higher):
- Low temperature (0.2): makes the model highly focused, deterministic and predictable. At 0.2, the model almost always selects the most statistically probable next words. It minimizes creative variation, off-topic drift and hallucinations.
- High temperature (0.7 to 1.0+): makes the model more creative, diverse and unpredictable, picking less likely words to introduce variety.
Why 0.2 is best for coding and agent tasks
Coding and agent work are the best use cases for a low temperature. Structured tool calls, JSON schemas and function names must be emitted exactly as defined, so determinism is a feature, not a limitation:
- Reliable tool calls. A low temperature keeps the model from improvising the syntax of tool calls, which is the whole point of the fix in this article.
- Reproducible output. The same prompt produces the same result, which matters when you compare runs or debug a pipeline.
- Fewer hallucinations. With less creative latitude, the model sticks to the code and instructions in its context instead of inventing functions or arguments.
- Cleaner code. Editing and refactoring benefit from the most probable, conventional tokens rather than stylistic surprises.
Reserve higher temperatures for brainstorming, creative writing or roleplay, where variety is the goal. For anything that must compile, run or parse, keep it at 0.2.
Step 2: enable tools in OpenCode
Open or create your OpenCode configuration file at ~/.config/opencode/opencode.json (or opencode.jsonc) and ensure "tools": true is explicitly declared for your new model:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"ollama": {
"npm": "@ai-sdk/openai-compatible",
"name": "Ollama local",
"options": {
"baseURL": "http://localhost:11434/v1"
},
"models": {
"qwen3-coder-32k": {
"name": "Qwen Coder 32K",
"tools": true
}
}
}
}
}
Step 3: clear the OpenCode cache and relaunch
Clear any cached provider sessions that might be holding onto truncated system prompts:
rm -rf ~/.cache/opencode
Launch OpenCode with Ollama:
ollama launch opencode --model qwen3-coder-32k
or run opencode directly from your project folder.
Alternative: force system prompt instructions
If the model still outputs raw XML tags after increasing the context length, add explicit system instructions to your Modelfile to force adherence to standard tool schemas:
FROM qwen3-coder
PARAMETER num_ctx 32768
PARAMETER temperature 0.2
SYSTEM """
You are an AI programming assistant.
When executing environment tools (such as explore, edit, or bash), ALWAYS use the exact tool call schema provided in the system prompt.
Do not output raw XML tags like <function=explore> or plain text function calls. Execute tool calls directly.
"""
Rebuild the model with ollama create qwen3-coder-32k -f Modelfile and restart OpenCode.
Conclusion
The root cause is almost always a truncated context window. Extending num_ctx restores the tool instructions that the model needs to return structured JSON instead of raw text.