gitagent icon indicating copy to clipboard operation
gitagent copied to clipboard

Custom OpenAI-compatible models send a 128k context and get truncated mid tool-call

Open enviable opened this issue 2 months ago • 0 comments

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

  1. Every custom:<model>@<baseUrl> model is hardcoded to a 128k window and a 32k completion budget in createCustomModel (dist/loader.js). Nothing reads the server's real context (/api/show, the Modelfile num_ctx, or agent.yaml).

  2. needsCompaction, truncateToolResults, and buildCompactPrompt in dist/compact.js are exported and never called. pi-agent-core will apply a transformContext, but the CLI Agent is constructed without one. The transcript grows until the server cuts it.

  3. model.constraints.max_tokens is copied onto the pi-ai request as maxTokens. For every provider except Chutes, getCompat in @mariozechner/pi-ai openai-completions.js sets maxTokensField to max_completion_tokens (useMaxTokens is only true when baseUrl contains chutes.ai). Ollama's OpenAI shim documents max_tokens and maps that field to num_predict (openai/openai.go, ChatCompletionRequest.MaxTokens). max_completion_tokens is not a field on that struct, so the constraint is dropped and generation runs until num_ctx.

  4. The built-in read tool 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.yaml set contextWindow for 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 only max_completion_tokens) for Ollama-compatible base URLs, since that is the field Ollama maps to num_predict.
  • Default tool-result truncation to something that fits the configured window, not a flat 100KB.

enviable avatar Sep 28 '26 17:09 enviable