Skip to content

fix(roadmap): a Claude stage reply stops at 4096 tokens #4220

Closed

Description

@dan-petty

Problem

A stage plan run on claude-sonnet-5-5 ends with StructuredOutputValidationError: 0 schema violation(s). Cause: the request sets no max_tokens, and the gateway sends the Anthropic default of 4096 output tokens. The reply is cut at 4096 tokens and holds no part.

Live run on 2026-11-09 (release 0.100.0 code, tool_choice auto, slot analysis, model claude-sonnet-5-5, window 200000, no fallback used): devops roadmap stage --plan --item 3599 made 15 requests, all answered by claude-sonnet-5-5.

  • Requests 1 to 13 offered 13 tools. The model made 2 to 5 tool calls in each. The input grew from 54,455 to 105,412 tokens.
  • Request 14 offered only the output tool final_result. The run counts the window with tokens_of, which errs high, and it passed tool_stop(200000) = 150,000 at a real input of about 105,000. The model still asked for read_file and search_code. Those tools were not offered, so pydantic-ai sent the retry prompt "Unknown tool name: 'read_file'. Available tools: 'final_result'". This used the one output retry (_REPLY_RETURNS = 1).
  • Request 15 ended with finish_reason length, 4096 output tokens and no part. pydantic-ai raised UnexpectedModelBehavior: "Model token limit (provider default) exceeded before any response was generated." The words "provider default" show that the request carried no max_tokens (last_max_tokens is None).
  • investigate (src/devops_cli/roadmap/refine/refine_investigation.py) turns this into InvestigationStopped(schema_broken=True), because the last reply holds no function tool call. ToolboxClient._stopped (src/devops_cli/roadmap/stage/stage_toolbox.py) raises StructuredOutputValidationError(violations=trace.violations). trace.violations counts only retry prompts whose content is a list. The retry prompt here is text, so the count is 0. A run that fails for any other reason without a list of violations also reads "0 schema violation(s)".

_claude_group_model (src/devops_cli/ai/pydantic_ai_bridge.py) builds the model with profile={"supports_forced_tool_choice": False} and no settings. The setting ai.max_tokens is read only for the Ollama path (_resolve_ollama_model). The gateway entry claude-sonnet-5-5 in k8s/llm/gateway/config.yaml sets no max_tokens. A StageProposal with the plan texts, the groups and the pull request body does not fit in 4096 tokens, so any Sonnet run that reaches its final reply with a full plan is cut. Not verified: the content of the cut reply (it reached the client as no part).

The fault was hidden before #4216: the call went to deepseek-v4.1-flash-1017 as the fallback (#4205). The 1 violation failures (#4004, #3576, #3592, #3612, #4028, #4043) are not explained by this run. Not verified.

Expected

A stage run on a Claude group can send a reply of the size of a plan. The message of a stop by the output limit names the limit, and not "0 schema violation(s)".

Acceptance criteria

  1. A test builds the model of a Claude gateway group with resolve_pydantic_ai_model("gateway:claude-sonnet-5-5") and asserts that model.settings["max_tokens"] is set to a value above 4096 (the value is a named constant). It fails today because model.settings holds no max_tokens.
  2. A test runs investigate with a model that answers finish_reason length with no part, and asserts that the message of the error names the output token limit. Today the error is StructuredOutputValidationError with 0 violations and a message that names none.
  3. uv run devops roadmap stage --plan --item 3599, with the Service config, does not stop with Model token limit (provider default). Today it does.

Existing tool considered

  • pydantic-ai ModelSettings(max_tokens=...) on OpenAIChatModel(settings=...) (library already in use, installed version has the settings argument). It covers the cause with one argument in _claude_group_model.
  • The same cap in the gateway entry (litellm_params.max_tokens) would also cover other callers of the group. Not verified that LiteLLM applies it as a default when the request sets none; decide with a live call. Do not copy the cap into both places.
  • fix(roadmap): a model call of an implement step has no output cap and a runaway reply holds the tier for 25 minutes #2869 (closed) put an output cap into the model settings of the implement agent. This issue follows that pattern.
  • No new tool is built.

Cost

Tokens to build: about 40,000 (one setting, a constant, two tests). Tokens to keep: none. Tokens to fix a second fault, if the run then shows one: unknown. Elapsed time: about 1 hour, plus one live stage run on Sonnet (about 2.5 USD, 15 requests of up to 106,000 input tokens).

Related

#4204 (placeholder replies; its follow-up cause was DeepSeek, #4205), #4205 (the fallback hid the 400), #4216 (tool_choice auto for a Claude group), #3529 (a return request and the window limit), #2869 (output cap of the implement agent).

Source: #4204

Proposed design

Problem statement

A stage plan run on the Claude group claude-sonnet-5-5 sends no max_tokens, so the gateway applies the Anthropic default of 4096 output tokens. The final reply of a plan does not fit in 4096 tokens, so the model output is cut and holds no part. The run then fails with StructuredOutputValidationError: 0 schema violation(s), a message that hides the real cause, the output token limit.

Acceptance criteria

Requirement: Claude group output cap

The model of a Claude gateway group SHALL set max_tokens in its model settings to a named constant above 4096.

Scenario: Claude group model has max_tokens

  • WHEN A test builds the model with resolve_pydantic_ai_model("gateway:claude-sonnet-5-5")
  • THEN model.settings["max_tokens"] equals the named constant, and the constant is above 4096

Requirement: Output limit stop names the limit

The error of an investigation that ends with finish_reason length and no part MUST name the output token limit in its message.

Scenario: Cut reply gives a message that names the limit

  • WHEN A test runs investigate with a model that answers finish_reason length with no part
  • THEN The error message names the output token limit and does not read "0 schema violation(s)"

Requirement: Other model stops keep their class

A stop that is not caused by the output token limit SHALL keep its current error class and message.

Scenario: Schema break without a limit stop

  • WHEN A test runs investigate with a model that breaks the schema of its reply and does not reach the output limit
  • THEN The error stays StructuredOutputValidationError with the same message as before

Existing Tool Considered

The library pydantic-ai, already in use, covers the cause: OpenAIChatModel(settings=ModelSettings(max_tokens=...)) sets the cap with one argument in _claude_group_model (src/devops_cli/ai/pydantic_ai_bridge.py:89). The same cap in the gateway entry (litellm_params.max_tokens) would also cover other callers, but nobody verified that LiteLLM applies it when the request sets none. No new tool is built. The cost of the own code is about 40,000 tokens to build (one setting, one constant, the error mapping and tests), no tokens to keep, and about 1 hour of elapsed time.

Key questions

  • Where does the cap go: the model settings in _claude_group_model, or the gateway entry in k8s/llm/gateway/config.yaml?: Put the cap in the model settings of _claude_group_model. The library documents the setting, the issue verified the cause in this code path, and a live check of the gateway default is not needed. Do not copy the cap into the gateway entry. (best practice) [sources: src/devops_cli/ai/pydantic_ai_bridge.py:81, k8s/llm/gateway/config.yaml, openspec/changes/archive/2026-10-31-2869-step-output-cap-and-call-timeout/design.md]
  • How does the error of a stop by the output limit name the limit?: In _stopped_outcome of refine_investigation.py, detect the UnexpectedModelBehavior whose message holds "token limit", or whose last reply has finish_reason length. Return an InvestigationStopped with schema_broken=False and a message that names the output token limit. Then ToolboxClient._stopped raises RoadmapStageError and not StructuredOutputValidationError. (best practice) [sources: src/devops_cli/roadmap/refine/refine_investigation.py, src/devops_cli/roadmap/stage/stage_toolbox.py]
    • Chosen: Detect the limit stop in _stopped_outcome and set schema_broken to false. The typed field already decides the error class in _stopped, so one change fixes the message for all callers of investigate. A budget-limit stop already has schema_broken false and a message that names the limit (change 3037).
    • Rejected: Count text retry prompts as violations in trace.violations. This would change the count for other failures and would not name the limit.
  • What value does the named constant hold?: Use 32000 as a named constant in src/devops_cli/config/constants.py (for example CONST_AI_CLAUDE_GROUP_MAX_OUTPUT_TOKENS). It is about 8 times the 4096 default, far above a plan with its groups and pull request body, and below the total stage output budget CONST_ROADMAP_STAGE_MAX_OUTPUT_TOKENS (50000 in change 3037), so the run's own output_tokens_limit still bounds the whole run. It equals the cap that change 3206 set for an implement step (32000). The Anthropic per-request maximum for Sonnet was not verified here (the docs page could not be fetched); the value is a cap and not a target, so a lower model maximum would show as a 400 in the live run of criterion 3. (best practice) [sources: openspec/changes/archive/2026-11-02-3206-raise-job-caps/proposal.md, openspec/changes/archive/2026-11-02-3037-the-stage-job-plans-with-a/tasks.md, openspec/changes/archive/2026-10-31-2869-step-output-cap-and-call-timeout/design.md]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    on Nov 9, 2026
  2. added
    type/bug
    scope/roadmap
    status/new
    value/high
    effort/low
    on Nov 9, 2026
  3. dan-petty commented on Nov 9, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/roadmap. The (roadmap) scope of the title gives the scope.
    • Priority: P1-High. Every stage run on a Claude model that reaches a full plan is cut and fails, blocking the workflow.
    • Value: High. It unblocks all Claude-based stage plan runs that produce replies above 4096 tokens.
    • Effort: Low. The fix is one settings argument in _claude_group_model plus a clearer error message and two tests.
    • Related: fix(k8s): the Claude gateway groups cannot serve a structured reply #3140, which asks for a different change. Both target Claude gateway groups and structured replies, but #3140 fixes tool_choice incompatibility while this sets max_tokens to lift the 4096 output cap.
  4. added
    priority/p1-high
    owner/agent
    on Nov 9, 2026
  5. added
    status/ready
    and removed
    status/new
    on Nov 9, 2026
  6. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 7411967 (see proposed design in issue body).

  7. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 9, 2026
  8. removed
    status/in-review
    owner/agent
    on Nov 9, 2026
  9. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor
  10. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-09 14:13 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p1-highscope/roadmapsource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions