Back to catalog / Agent workflows
Continue a response stopped by the output limit
Detect max_tokens termination and continue generation without duplicating or silently truncating the result.
Goal
Detect `max_tokens` termination and continue generation without duplicating or silently truncating the result.
Steps
- Inspect
stop_reasonfromclient.messages.create; continue only when it equalsmax_tokens, notend_turn. [source09] - Append the assistant response and a continuation prompt to
messages, then request the next segment with a bounded retry count. [source09] - Join segments and test boundary cases where output ends mid-sentence or exactly at a section boundary; surface a clear incomplete status on exhaustion. [source09]
How to verify
Force a short max_tokens limit; confirm continuation runs only after stop_reason=max_tokens, stops at end_turn, and reports incomplete when the retry cap is reached.
Troubleshooting
If a segment repeats or gets appended after end_turn, gate continuation on stop_reason and keep a bounded retry counter with an explicit incomplete result.
Permissions
Prerequisites
- Python and Anthropic API key; set an application-level maximum response size and retry budget.
Permissions
- Use approved test or anonymized data only; scope credentials to each workflow, and require explicit approval for external writes.
Limitations
Model output may be incomplete or wrong; API and beta behavior can change, so verify against the pinned source and current service behavior.
Pinned upstream source
- Repository
- anthropics/anthropic-cookbook
- File path
- misc/sampling_past_max_tokens.ipynb
- Pinned commit
9c1e493ddd2b82df399d2a61b9c806c539e18be6- Source checked
- 2026-10-14 · Reviewed against the pinned upstream source
Repository metrics
These values describe the upstream repository, not popularity or likes for an individual skill or playbook.
- Stars
- Not collected
- Forks
- Not collected
- Observed at
- Not collected
No metrics snapshot is available yet.
License: MIT
Main topic
Take a coding Agent change from context to verification