Agent Wealth HubFree public resources
中文 EN العربية

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

  1. Inspect stop_reason from client.messages.create; continue only when it equals max_tokens, not end_turn. [source09]
  2. Append the assistant response and a continuation prompt to messages, then request the next segment with a bounded retry count. [source09]
  3. 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

Permissions

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

https://github.com/anthropics/anthropic-cookbook/blob/9c1e493ddd2b82df399d2a61b9c806c539e18be6/misc/sampling_past_max_tokens.ipynb

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

Related resources