agent-native icon indicating copy to clipboard operation
agent-native copied to clipboard

Final answer streamed twice when the model calls suggest-follow-ups in the same step

Open janluke opened this issue 1 month ago • 1 comments

Version: @agent-native/core 0.209.2; the same code is on main (c4a3ea4d) and in 0.210.0. Model claude-haiku-5-5 through the Anthropic engine.

What happens: on some turns the agent streams its final answer twice, as two slightly different versions, in the same turn (2 of 4 runs of the same one-line question). Each affected turn also costs an extra model request.

SSE events of a failing turn (text abbreviated):

[model_stream start][end: tool_use]  tool_start: spending-by-month
[model_stream start][end: tool_use]  tool_start: list-transactions
[model_stream start] text: "No transactions in October 2026, total 0 €." [end: tool_use]   <- answer + suggest-follow-ups
[model_stream start] text: "There are no transactions in October 2026, so total spending is 0 €." [end: end_turn]
done

Cause: in packages/core/src/agent/production-agent.ts, when a step's only tool call is suggest-follow-ups and terminalStopReason === "tool_use", the loop runs flushUnstreamedAssistantText(); continue;, so the text is treated as a preamble and the model is asked again (spec: "preserves provider tool_use continuation when publication accompanies a preamble").

The single-request path for "final reply + suggestions in one step" (spec: "publishes grounded metadata before done without a second model request or visible tool activity") is only reached with stop reason end_turn. Real providers don't produce that combination: anthropic-engine.ts passes through Anthropic's stop_reason, which is always tool_use when the message contains a tool call, and translate-ai-sdk.ts maps tool-calls to tool_use. So with Claude (and AI SDK providers) that path is effectively unreachable, and the model often repeats its answer in the extra step.

Expected: one visible answer and no extra request when a step contains final-answer text plus only suggest-follow-ups. Possible approaches: treat that combination as final regardless of the provider stop reason, or, if the continuation is kept for provider pairing, send the follow-up completion instruction ("the final reply above has already been delivered... do not repeat the reply") instead of an open continuation.

Repro: any app on the chat template with the Anthropic engine; ask a question that needs one or two tool calls and log /_agent-native/agent-chat events. It doesn't happen every time, since it depends on whether the model calls suggest-follow-ups in the same step as the answer.

janluke avatar Oct 19 '26 20:10 janluke

Thanks for the detailed report. The follow-up response behavior was addressed in https://github.com/BuilderIO/agent-native/pull/8347, which is merged; the original incident itself was not reproduced.

builder-factory avatar Nov 06 '26 13:11 builder-factory