Why Streaming Matters
When you call query(), you wait for the entire response before seeing anything. For short tasks, that is fine. But when an agent is reading files, running commands, and thinking for 30 seconds, your user stares at a blank screen. Streaming fixes that by sending you each piece of the response as it happens — word by word, tool by tool, event by event.
The Event Stream
When you use streaming mode, the SDK emits a sequence of events that tell you exactly what is happening inside your agent. Each event has a type and a payload:
Streaming in Practice
The SDK provides a streaming interface that lets you handle events as they arrive. Here is a complete example:
import { Claude } from "@anthropic-ai/claude-agent";
const agent = new Claude({
model: "claude-sonnet-4-6",
tools: "defaults",
});
// Use the streaming query method
const stream = agent.streamQuery(
"Read package.json and summarize the project."
);
// Listen for events as they arrive
for await (const event of stream) {
switch (event.type) {
case "text_delta":
// A chunk of text — print it immediately
process.stdout.write(event.text);
break;
case "tool_use":
// Claude is calling a tool
console.log(`\n[Tool: ${event.name}]`);
break;
case "tool_result":
// Tool finished — show a brief status
console.log(`[Tool complete]\n`);
break;
case "result":
// Final result with metadata
console.log(`\nCost: $${event.cost}`);
break;
}
}