Custom OpenAI-compatible models send a 128k context and get truncated mid tool-call
What happens
gitagent 2.2.0 against a local Ollama model whose Modelfile sets num_ctx 65536 sends the whole transcript every turn. The server then stops the completion at the context limit, often inside a tool-call argument, and the session sits there for minutes looking frozen.
Observed on 2026-09-28 against gemma4-claude (PARAMETER num_ctx 65536), Ollama journal:
slot operator(): new prompt, n_ctx_slot = 65536, task.n_tokens = 65405
slot release: stop processing: n_tokens = 65535, truncated = 1
level=WARN msg="gemma4 tool call flush on done failed" error="unexpected end of JSON input" content=call:cli{
[GIN] POST "/v1/chat/completions" | 200 | 3m7s
slot operator(): new prompt, n_ctx_slot = 65536, task.n_tokens = 65302
slot release: stop processing: n_tokens = 65535, truncated = 1
[GIN] POST "/v1/chat/completions" | 200 | 4m9s
Prompt cache reuse on the second call was about 5k tokens (n_past = 4785), so about 60k tokens were recomputed. That is the multi-minute stall. The truncated = 1 stop is why the following tool call is malformed.
Why gitagent does not prevent it
-
Every
custom:<model>@<baseUrl>model is hardcoded to a 128k window and a 32k completion budget increateCustomModel(dist/loader.js). Nothing reads the server's real context (/api/show, the Modelfilenum_ctx, oragent.yaml). -
needsCompaction,truncateToolResults, andbuildCompactPromptindist/compact.jsare exported and never called.pi-agent-corewill apply atransformContext, but the CLIAgentis constructed without one. The transcript grows until the server cuts it. -
model.constraints.max_tokensis copied onto the pi-ai request asmaxTokens. For every provider except Chutes,getCompatin@mariozechner/pi-aiopenai-completions.jssetsmaxTokensFieldtomax_completion_tokens(useMaxTokensis only true whenbaseUrlcontainschutes.ai). Ollama's OpenAI shim documentsmax_tokensand maps that field tonum_predict(openai/openai.go,ChatCompletionRequest.MaxTokens).max_completion_tokensis not a field on that struct, so the constraint is dropped and generation runs untilnum_ctx. -
The built-in
readtool allows 2000 lines or about 100KB (dist/tools/shared.js,MAX_LINES/MAX_BYTES). One result is enough to fill a 32k or 64k local context.
What would fix it
- Let
agent.yamlsetcontextWindowfor custom models, or query the OpenAI-compatible server for it, and default well under 128k when the base URL is loopback. - Call the existing compaction helpers from the agent loop once the estimate crosses about 75% of that window, before the request is sent.
- Send
max_tokens(not onlymax_completion_tokens) for Ollama-compatible base URLs, since that is the field Ollama maps tonum_predict. - Default tool-result truncation to something that fits the configured window, not a flat 100KB.