Continue model turns that Anthropic pauses with stop_reason: "pause_turn".
Anthropic runs server tools such as web search and web fetch in a loop of its
own and can pause a long turn before it finishes. The response then ends with
stop_reason: "pause_turn", sometimes with a server tool call that has not
run yet, and carries no client tool calls, so the agent loop would end the run
on the unfinished turn.
This middleware sends a paused response back unchanged, with the same tools
and no added user message, until the turn ends. Each continuation is a
separate request, and a turn still paused after maxContinuations of them
throws PauseTurnLimitExceededError, which carries the responses of
the turn.
The responses of a continued turn are returned as one AIMessage whose
content is every response's content in order, so the paused content stays in
the history that later requests send back. It keeps the last response's id
and metadata and sums the usage of all of them. Anthropic merges consecutive
assistant turns, so the model sees the same conversation either way.
Responses from models other than Anthropic's never pause, so they pass through unchanged.
Tools that run in a code execution container, including the _20260209 and
later versions of web search and web fetch, need
new ChatAnthropic({ reuseLastContainer: true }) so continuations run in the
same container as the paused turn.
anthropicPauseTurnMiddleware(
middlewareOptions: AnthropicPauseTurnMiddlewareConfig = {}
): AgentMiddleware<undefined, undefined, unknown, readonly ClientTool | ServerTool[], readonly []>| Name | Type | Description |
|---|---|---|
middlewareOptions | AnthropicPauseTurnMiddlewareConfig | Default: {}Configuration options |
import { createAgent, anthropicPauseTurnMiddleware } from "langchain";
const agent = createAgent({
model: "anthropic:claude-sonnet-5-5",
tools: [{ type: "web_search_20250305", name: "web_search" }],
middleware: [anthropicPauseTurnMiddleware()],
});