DEV Community

Cover image for Smaller Requests for Claude Agent Loops – If You Plan for Replay
Sascha Mirow-Stanitzok
Sascha Mirow-Stanitzok

Posted on Edited on Originally published at sascha-ms.de

Smaller Requests for Claude Agent Loops – If You Plan for Replay

If you have built an agent loop on the Claude Messages API, you know this pattern: Claude calls a tool, you return the result, Claude calls the next tool. Because the API is stateless, every single round sends the entire conversation again – system prompt, tool definitions and all previous turns. After twenty steps, a large JSON payload crosses the wire just so Claude can read one new tool result.

Since November 2, 2026, the message threads beta addresses exactly that. I tried it with a small agent and wrote a complete TypeScript walkthrough of the Claude message threads API. Here is what you need to know before you adopt it.

The idea in one paragraph

On the first request you send everything as usual and ask the API to create a thread. The API keeps the conversation together with Claude's answer for a short time. On the next request you only send the new messages and reference the previous reply via previous_message_id, leaving out system and tools. According to Anthropic, this lowers the time to first token and keeps it steadier in long conversations. Prices and rate limits stay the same – threads save transfer volume and latency, not tokens. For cost savings, prompt caching is still your tool, and you have to configure it yourself.

The part that will bite you: threads disappear

A thread expires five minutes after the last request and is deleted no later than an hour after its last use. Anthropic explicitly calls it best-effort. So there are really three cases, not two:

  • Create: first request with full conversation, system prompt and tools
  • Continue: only the new messages plus the previous reply's ID
  • Replay: the thread is gone, so you resend the full conversation from your own copy and continue with the new ID

If you do not build the replay path from day one, your application simply fails after a five-minute pause. That means your application must keep its own copy of the conversation – it remains the source of truth, and the API only caches it. In my example class, only a specific thread error code triggers a replay, so an unrelated 404 like a wrong model name does not end up in an endless loop. And if recreating fails too, the class falls back to plain requests without a thread: slower, but still working.

A replay path that rarely runs is a path that will quietly break one day. That is why I wrote tests that force exactly this case, using a fake fetch – no API key needed.

When threads are worth it

Threads shine for agents that call tools every few seconds: many short rounds, a growing context, small pauses. For chats with humans I would not start with them – anyone who goes to get a coffee breaks the five-minute window, and the replay eats the gain.

There are also hard exclusions. Threads are neither ZDR nor HIPAA eligible, so check with your data protection people if zero data retention is in your contract. Message Batches and token counting reject threads. And Amazon Bedrock is not listed as a supported platform; the beta runs on the Claude API, Claude Platform on AWS, Google Cloud (Anthropic managed) and Microsoft Foundry.

Rules inside a running thread

A thread stores a fingerprint of certain settings. Change model, tool_choice, thinking or a few other fields mid-thread and you get HTTP 400 with thread_fingerprint_mismatch. Fields like max_tokens or stream may change freely. A few more pitfalls I ran into while reading the docs:

  • The API does not deduplicate – sending old messages on continue corrupts the thread.
  • When streaming, continue only after message_stop, not after message_start.
  • After a refusal, start fresh instead of continuing.
  • Threads are append-only, so compaction means summarizing on your side and opening a new thread.
  • Threads belong to the API key that created them, so rotating keys forces replays.

My take: I like how honestly the feature is designed. It is a short-lived accelerator, not a conversation store, and that matches how I would build agents anyway.

What the full article covers

  • A tested ThreadedConversation class in TypeScript with automatic replay and fallback
  • An agent loop with a bike rental tool that only sends tool results on continue
  • Six tests with a fake fetch function that run without network access
  • The full table of fixed, create-only and free fields, plus prompt caching rules for threads
  • Common errors, an FAQ and my view on data protection

Read the full article here: Claude Message Threads API: Agent Loops in TypeScript

📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.


👉 Next step: New here? My start page collects the best articles, tools and learning paths in one place.

📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).

Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en

Top comments (0)