Skip to content

fix(anthropic): warn when response is truncated by max_tokens limit#2212

Open
citizen204 wants to merge 1 commit into
griptape-ai:mainfrom
citizen204:fix/anthropic-max-tokens-warning
Open

fix(anthropic): warn when response is truncated by max_tokens limit#2212
citizen204 wants to merge 1 commit into
griptape-ai:mainfrom
citizen204:fix/anthropic-max-tokens-warning

Conversation

@citizen204

@citizen204 citizen204 commented Jun 23, 2026

Copy link
Copy Markdown

Summary

Closes #1982

When the Anthropic API stops generation because the response hit the max_tokens limit, users currently receive silently truncated output with no indication of what happened. This PR adds a WARNING-level log message in both the streaming and non-streaming paths so the problem is immediately visible.

  • In try_run: checks response.stop_reason == "max_tokens" and logs a warning with the current max_tokens value.
  • In try_stream: checks event.delta.stop_reason == "max_tokens" in the message_delta event and logs the same warning.

No behaviour changes — the message is still returned as-is; users who want longer output can simply increase max_tokens.

Test plan

  • test_try_run_warns_on_max_tokens — mocks a response with stop_reason="max_tokens", asserts a WARNING containing "max_tokens" is emitted.
  • test_try_stream_warns_on_max_tokens — mocks a stream with a message_delta whose delta.stop_reason="max_tokens", asserts same warning.
  • Existing test_try_run / test_try_stream_run tests are unaffected (mock responses have a non-string stop_reason that never equals "max_tokens").

📚 Documentation preview 📚: https://griptape--2212.org.readthedocs.build//2212/

Log a WARNING when the Anthropic API returns stop_reason "max_tokens"
so users understand why output is cut short and know to raise max_tokens.
Applies to both try_run (non-streaming) and try_stream paths.

Closes griptape-ai#1982

@collindutter collindutter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@citizen204 this was made against the dev branch, please make the PR against the main branch.

@citizen204 citizen204 changed the base branch from dev to main June 23, 2026 19:38
@citizen204

Copy link
Copy Markdown
Author

Hi @collindutter — just to flag: the PR is currently targeting main (you can verify in the PR header). Happy to address any other concerns you have! Let me know if there's anything else to adjust.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic Prompt Driver is Truncating Output - possibly due to the default of 1000 on max_tokens

2 participants