Skip to content

AI Agent responses get cut off mid-answer — no maxOutputTokens set, provider falls back to 4096 #539

Closed

Description

@gkhngyk

Problem

Users report the AI Agent (Dashboard → Agent) cutting off long answers mid-sentence.

Root cause chain:

  1. The agent chat route calls streamText without setting maxOutputTokens (web/src/app/api/agent/chat/route.ts, model built in web/src/lib/agent/model.ts).
  2. The installed @ai-sdk/anthropic (3.0.81) has no entry for claude-sonnet-5 in its known-model capabilities table, so the model falls into the unknown-model branch with a default of maxOutputTokens: 4096 — that's the max_tokens sent to the Anthropic API.
  3. On claude-sonnet-5, adaptive thinking is on by default when the thinking parameter is omitted, and thinking tokens count against the same max_tokens budget. The visible answer therefore gets significantly less than 4096 tokens, and long responses stop with stop_reason: max_tokens mid-sentence.

Fix

Add an explicit output cap to the streamText call in web/src/app/api/agent/chat/route.ts:

const result = streamText({
  model: buildAgentModel(anthropicKey),
  maxOutputTokens: 16000,
  // ...existing options unchanged
});

Notes:

  • The route already streams, so a higher cap carries no timeout risk; the model still stops naturally when the answer is done — this only raises the ceiling.
  • The limit applies per generation step in the tool loop (stopWhen: stepCountIs(20)), so the truncation showed up mostly on long final answers.
  • Cost is on the org's own BYOK Anthropic key and scales with actual usage, not with the cap.

Acceptance criteria

  • Long agent answers complete instead of stopping mid-sentence.
  • Tool-calling behavior and token-usage recording (recordAgentTokenUsage) are unchanged.
  • yarn lint / yarn typecheck / yarn format:check pass from web/.

Activity

  1. added
    bug
    good first issue
    on Jul 28, 2026
  2. BharadwajKanneveti commented on Jul 28, 2026

    @BharadwajKanneveti
    Contributor

    Hi @gkhngyk I will work on this issue

  3. gkhngyk commented on Jul 30, 2026

    @gkhngyk
    ContributorAuthor

    Assigned to you @BharadwajKanneveti — looking forward to the PR!

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

Metadata

Metadata

Labels

buggood first issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions