AI & LLM APIs cookbook · Intermediate
Fit conversation history into a token budget
Long chats eventually exceed the context window or cost too much; trim history before sending.
Approach
Count tokens with client.messages.count_tokens before sending. If the request exceeds your budget, drop the
oldest turns (keeping the first user message for context) or replace them with a short summary.
Solution
import anthropic
client = anthropic.Anthropic()
MODEL = "claude-sonnet-5-5"
BUDGET = 30_000 # input tokens you are willing to send per call
def count(system: str, messages: list[dict]) -> int:
return client.messages.count_tokens(model=MODEL, system=system, messages=messages).input_tokens
def fit_history(system: str, messages: list[dict]) -> list[dict]:
trimmed = list(messages)
while count(system, trimmed) > BUDGET and len(trimmed) > 3:
# drop the oldest user/assistant pair after the first message, keeping turns alternating
del trimmed[1:3]
return trimmed
def reply(system: str, history: list[dict]) -> str:
messages = fit_history(system, history)
response = client.messages.create(model=MODEL, max_tokens=1000, system=system, messages=messages)
return response.content[0].text
Pitfalls
- Breaking the user/assistant alternation (or orphaning a tool result) when deleting turns.
- Counting with a characters-divided-by-four estimate for hard limits; use the counting endpoint.
- Trimming the system prompt instead of old turns.
Variations
- Summarise dropped turns with a small model and insert the summary as context.
- Store the full history in your database and send only a window.
Further reading: Token counting
Reference: https://docs.anthropic.com/en/docs/build-with-claude/token-counting
More AI & LLM APIs recipes
- Answer with citations that point at the source text
- Build hybrid RAG search on PostgreSQL with pgvector
- Cache a long system prompt and documents
- Cache answers to frequent questions safely
- Classify thousands of items with the Batch API
- Compare two prompt versions with an unbiased model judge
- Expose an internal API as an MCP server
- Extract structured data with a schema