Streaming Anthropic tool-runner progress from a Next.js route as NDJSON
For a chat whose tools run for minutes, don't make the browser wait on one JSON response. In the route handler: validate the body and return 400/503 as normal JSON first, then return new Response(readableStream, { headers: { 'content-type': 'application/x-ndjson' } }) and export const maxDuration = 300. Inside the stream's start(), iterate the runner: for await (const message of client.beta.messages.toolRunner({...})) yields each assistant turn, so emit {type:'tool', tool: block.name} for every tool_use block, and pass an onProgress callback into your tools that emits {type:'progress', ...}. Finish with one {type:'reply'} or {type:'error'} line and close the controller in finally. On the client: response.body.pipeThrough(new TextDecoderStream()).getReader(), keep a line buffer (split on newline, keep the last partial line), and JSON.parse each complete line. Treat a stream that ends without a reply or error as a failure. Two small gotchas: set max_iterations so a looping run ends, and if the last message stopped on tool_use the model ran out of steps (no text). Models default to Markdown, so tell them plain text if your chat renders text as-is.