fix(anthropic): warn when stop_reason='max_tokens' silently truncates output#5717
Open
Beandon13 wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix(anthropic): warn when stop_reason='max_tokens' silently truncates output#5717Beandon13 wants to merge 1 commit intocrewAIInc:mainfrom
Beandon13 wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
The Anthropic Message API returns stop_reason='max_tokens' when output is cut off before generation finished, but AnthropicCompletion previously discarded the field after extracting token usage. Silent truncation in the final synthesis response (after tool use) is especially harmful because partial JSON / answers are passed downstream as "complete". Surface the signal as a logging.warning at all four completion sites (sync/async, plain completion + tool-use follow-up). The warning includes the agent role and current max_tokens for actionable triage. Closes crewAIInc#5148
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AnthropicCompletionpreviously discardedresponse.stop_reasonafter extracting token usage, so callers had no way to detect that a Claude response was cut off bymax_tokens. The follow-up call inside_handle_tool_use_conversationis the most painful site because the truncated synthesis answer is what downstream code parses._warn_on_max_tokens_truncation, and calls it at all four completion sites (sync/async × plain completion / tool-use follow-up). Whenstop_reason == "max_tokens"we emitlogging.warning(...)including the agent role (when available) and the configuredmax_tokensso users can act on it.Closes #5148
Testing
Existing token-usage tests in the same file (
test_anthropic_token_usage_tracking,test_anthropic_cache_creation_tokens_extraction,test_anthropic_missing_cache_fields_default_to_zero,test_anthropic_completion_is_used_when_anthropic_provider) also pass unchanged.