Streaming, tools, JSON, and prompt caching
Production patterns for the provod.ai text API: SSE streaming with usage, function calling, strict JSON Schema, image inputs, and prefix caching.
Last updated on
Most production applications build on top of POST /v1/chat/completions, POST /v1/responses, or POST /v1/messages using five core capabilities of the API: SSE streaming, external tool calling, strict JSON Schema, vision inputs, and prompt prefix caching. Check parameter support for your chosen model in the model catalog.
All four mechanisms run through
https://api.provod.ai/v1 with standard OpenAI and Anthropic SDKs.
1. SSE streaming with usage reporting
Enable stream: true and stream_options: { "include_usage": true } to stream output as it arrives and receive final token counts in the last event before [DONE].
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.provod.ai/v1",
api_key=os.environ["PROVOD_API_KEY"],
)
stream = client.chat.completions.create(
model="openai/gpt-5.4",
messages=[{"role": "user", "content": "Write a REST API launch checklist."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.usage:
print(f"\n\nTotal tokens: {chunk.usage.total_tokens}")2. Tool calling
Pass function definitions in the tools array. When the model selects a tool, it returns finish_reason: "tool_calls" with JSON arguments. Your application runs the function locally and returns the result in a message with role: "tool".
import json, os
from openai import OpenAI
client = OpenAI(base_url="https://api.provod.ai/v1", api_key=os.environ["PROVOD_API_KEY"])
tools = [{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Returns the order status by order ID",
"parameters": {
"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"],
"additionalProperties": False,
},
},
}]
messages = [{"role": "user", "content": "Check status for order ORD-4821"}]
first = client.chat.completions.create(model="openai/gpt-5.4", messages=messages, tools=tools)
msg = first.choices[0].message
if msg.tool_calls:
call = msg.tool_calls[0]
args = json.loads(call.function.arguments)
tool_result = {"order_id": args["order_id"], "status": "delivered"}
messages.extend([
msg,
{"role": "tool", "tool_call_id": call.id, "content": json.dumps(tool_result)},
])
final = client.chat.completions.create(model="openai/gpt-5.4", messages=messages, tools=tools)
print(final.choices[0].message.content)3. Structured outputs
For data extraction, ticket classification, and database integration, pass response_format with type: "json_schema" and strict: true (verify structured_outputs is listed under supported_parameters in GET /v1/models):
{
"model": "openai/gpt-5.4",
"messages": [
{"role": "user", "content": "Extract company name and tax ID: Acme LLC, Tax ID 7701234567"}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "company_extract",
"strict": true,
"schema": {
"type": "object",
"properties": {
"company_name": {"type": "string"},
"inn": {"type": "string"}
},
"required": ["company_name", "inn"],
"additionalProperties": false
}
}
}
}4. Vision inputs
Include images inside user message content as a URL or data URI (data:image/png;base64,...) for models whose input_modalities includes image:
{
"model": "google/gemini-3.1-pro-preview",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Describe the diagram in the image and list its nodes."},
{"type": "image_url", "image_url": {"url": "https://example.com/diagram.png"}}
]
}
],
"max_tokens": 500
}5. Prompt prefix caching
In agent loops, RAG, and long conversations, the unchanging prefix of a prompt (system instructions, tools definitions, reference text) can hit the prefix cache:
- Place static instructions and tool definitions at the start of the request, and mutable user input at the end.
- In Anthropic Messages API, attach
cache_control: { "type": "ephemeral" }to the stable system block or final static message. - Compare
cached_tokens(OpenAI shape) orcache_read_input_tokens(Anthropic shape) across two consecutive requests. Check whether the selected model publishes a separate cache price in the model catalog and inspect final settlement throughGET /v1/generation.