SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
3.1 KB · 38 lines markdown
Rendered Raw Blame History
1# Streaming tool calls and fine-grained tool streaming23**Status:** DOCUMENTED · LIVE_VERIFIED 2026-09-18 (b1 buffered, b2 `eager_input_streaming:true`, b3 legacy header; claude-haiku-4-5-20251001).4**Sources:** [Fine-grained tool streaming](https://platform.claude.com/docs/en/agents-and-tools/tool-use/fine-grained-tool-streaming) · [Streaming messages](https://platform.claude.com/docs/en/build-with-claude/streaming) · [Server tools — streaming](https://platform.claude.com/docs/en/agents-and-tools/tool-use/server-tools#streaming-server-tool-events).5**Last verified:** 2026-09-18.67## Event sequence for a `tool_use` block (live b1, `tool_choice: any`)89```10message_start            usage.input_tokens=67611content_block_start [0]  {"type":"tool_use","id":"toolu_01VJ5…","name":"get_weather","input":{},"caller":{"type":"direct"}}12ping13content_block_delta [0]  {"type":"input_json_delta","partial_json":""}14content_block_delta [0]  {"type":"input_json_delta","partial_json":"{\"locat"}15content_block_delta [0]  {"type":"input_json_delta","partial_json":"ion\": \""}16content_block_delta [0]  {"type":"input_json_delta","partial_json":"San Fra"}17content_block_delta [0]  {"type":"input_json_delta","partial_json":"ncisco,"}18content_block_delta [0]  {"type":"input_json_delta","partial_json":" CA\"}"}19content_block_stop  [0]20message_delta            {"stop_reason":"tool_use","stop_sequence":null,"stop_details":null,"container":null} usage.output_tokens=4121message_stop22```2324Accumulate `partial_json` strings and `json.loads` at `content_block_stop`. `input: {}` in `content_block_start` is a placeholder. `message_delta.delta` carries `stop_details` and `container` fields (live; `container` is set when code execution ran).2526## Fine-grained (eager) streaming2728Set `"eager_input_streaming": true` on a **custom** tool (per tool; `false` keeps buffered streaming even when the legacy header is present). No beta header needed since 2026-02-05; the legacy `anthropic-beta: fine-grained-tool-streaming-2025-05-14` still works and enables it for tools that leave the field unset (b3 produced the same fragments as b2). The header is **rejected** together with `computer_toolset_20260801` / `browser_toolset_20260801` entries.2930| Mode | Fragments seen live (same prompt) | Guarantees |31|---|---|---|32| buffered (default) | 6 short chunks, each key/value validated before emission; delays between events | accumulated string is valid JSON |33| eager | 4 longer chunks (`{"location": "San`, ` Francisco, CA`, `"}`) | **no** server-side buffering or validation → may be partial/invalid JSON, especially at `stop_reason: max_tokens`; guard the parse and return `{"INVALID_JSON": raw}` as an `is_error` tool_result |3435Server tools stream the same way: `server_tool_use` gets `input_json_delta`s, the `*_tool_result` block arrives whole in a single `content_block_start` (no deltas; pause while the tool runs, `ping` keepalives ~30 s). Toolset members always arrive as one complete `input_json_delta`.3637Examples: `examples/anthropic/tools/fine-grained-streaming/basic.{sh,py}`. Test: `tests/anthropic/test_tools.py::test_streaming_input_json_delta`.38