Skip to main content
This page extends native Claude integration with one Python file supporting text SSE and function tool loops. Both modes consume SSE and print the final visible answer only after validation; partial text is not displayed as it arrives. Confirm native streaming and function support with your deployment administrator.

Setup and execution

Use Python 3.10+ with the standard library; no provider SDK is required. Set your gateway token in API_KEY, then use a root URL without /v1 or /v1beta. Deployment path prefixes are supported.
Run from the documentation directory, or omit examples/ if you saved the file separately. The first command requests a greeting; the second offers multiply, asks for 7 × 8, and returns the calculation to the model for its final answer.
API_NATIVE_MODE defaults to text. In tools mode, a model may also answer directly. Test responses are fixed fixtures; real wording and tool selection are not guaranteed.

Events and the tool loop

Only tool_use enters tool execution; only end_turn accepts a final answer without tools. Token-limit stops, refusals, missing completion, and stream errors fail. The fixed max_tokens: 1024 is a local budget requiring model support; complex requests can still truncate. The example accumulates thinking and signature deltas into the original thinking block, preserving redacted data and block order for tool-round replay. Only text is displayed. Thinking is not explicitly enabled, but supported thinking blocks returned by a channel are retained. Missing signatures fail rather than being synthesized or modified.

Complete Python example

Failure and resource boundaries

The SSE reader handles incremental UTF-8, LF / CRLF / CR, comments, and multiline data. It ignores SSE id/retry fields rather than replaying a request automatically. Socket operations time out after 60 seconds; this is not a deadline for the entire workflow. Each turn allows at most 512 data events, 4096 lines, and 1,048,576 decoded characters, with 65,536 characters per line. These are example safeguards, not gateway limits. Only the local pure calculation multiply is allowed. Arguments must contain exactly finite numeric a and b; booleans, extra fields, and nonfinite results fail. At most three model requests and four calls per turn are allowed. A third turn requesting more tools fails. All results are prepared before another request. Tools with side effects need separate authorization, idempotent execution, and recovery; multiplication is not a transaction design. HTTP failures, redirects, wrong content types, malformed JSON, truncated SSE, broken connections, unsuccessful completion, and unsupported extensions exit nonzero. Credentials, upstream error bodies, thinking text, and incomplete answers are not printed. Unknown events or blocks fail explicitly until the parser is extended to support them. Claude additionally limits each turn to 16 content blocks and each argument string to 16,384 characters. Server tools, citation deltas, and unlisted blocks are outside this example.

Verification

Run npm run check:examples. Tests execute this script against a local HTTP fixture, covering text/tool modes, complete history and signatures, result association, split UTF-8, errors, disconnects, no retries, and second-turn failure short-circuiting. No real models are called; actual event delivery and signature preservation require deployment testing. Claude streaming events · Tool results 阅读中文