Skip to content

Streaming, tools, and caching

Set stream: true in a generation request. Inference returns text/event-stream, and each API format uses its own event schema.

Format What to read
Chat Completions choices[].delta for incremental output. [DONE] signals the end. Set stream_options.include_usage: true to receive the usage frame.
Responses response.output_text.delta for incremental text and the terminal response event for the result and usage.
Anthropic Messages content_block_delta for incremental content, usage across message_start and message_delta, and message_stop at the end.

Use an SDK’s event parser or buffer until a complete SSE event has arrived. Individual network chunks can end in the middle of an event. Replace YOUR_API_KEY with your Inference key to run the example.

Terminal window
curl --no-buffer --fail-with-body https://inference.valgix.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
--data '{
"model": "deepseek-v4.1-flash-uncensored",
"messages": [{"role": "user", "content": "Explain what a prompt cache stores."}],
"max_tokens": 256,
"stream": true,
"stream_options": {"include_usage": true}
}'

Declare function tools with the selected format’s tools field. The response can contain tool calls with names, arguments, and call identifiers. Your application validates the arguments, runs the selected function, then returns its result in a follow-up request.

Format Read tool calls from Return the result as
Chat Completions message.tool_calls or streamed delta.tool_calls A message with role: "tool" and the original tool_call_id
Responses function_call output items A function_call_output input item with the original call_id, alongside the preceding context
Anthropic Messages tool_use content blocks A tool_result block with the original tool_use_id

For streaming tool calls, assemble arguments from the appropriate deltas before parsing JSON. Preserve the model’s tool-call identifiers and the prior conversation when you send results back.

The following request gives the model a function for looking up a fictional delivery status. The function runs in your application.

{
"model": "deepseek-v4.1-flash-uncensored",
"messages": [{"role": "user", "content": "Look up the status of order 1042."}],
"tools": [
{
"type": "function",
"function": {
"name": "lookup_delivery",
"description": "Return the delivery status for a given order number.",
"parameters": {
"type": "object",
"properties": {"order_number": {"type": "string"}},
"required": ["order_number"]
}
}
}
],
"max_tokens": 512
}

After running the function, send the original messages, the returned assistant message with its tool calls, and a tool result with the matching ID. For example, when the assistant returned a call with ID call_42, append this result immediately after that assistant message.

{"role": "tool", "tool_call_id": "call_42", "content": "{\"status\":\"in transit\"}"}
Format Request settings Where to read reasoning
Chat Completions reasoning_effort message.reasoning_content
Responses reasoning.effort Reasoning items in output
Anthropic Messages Enable thinking and set output_config.effort where supported thinking content blocks

For Chat Completions, use low, high, or max to request an effort level. none requests the lowest reasoning level the model supports. Reasoning can count toward the output limit, so leave room for the final answer. Preserve reasoning fields returned on assistant tool calls when replaying those messages.

Prompt caching is automatic when a matching prefix is available. Subsequent requests can reuse that prefix when they start with the same token sequence. Keep stable instructions, tool definitions, and shared context first. Append turn-specific input afterward.

Format Cached input tokens
Chat Completions usage.prompt_tokens_details.cached_tokens
Responses usage.input_tokens_details.cached_tokens
Anthropic Messages usage.cache_read_input_tokens

Cached input is included in Total input in the dashboard. The Of which cached count shows how much of that input came from the cache.