# Streaming, tools, JSON, and prompt caching

Source: https://provod.ai/en/docs/streaming-tools

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](/en/products/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](/en/models).

*Four production patterns of the provod.ai text API: SSE streaming, Tool Calling, Structured Outputs, and prompt caching.*
&#x2A;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]`.




Python



Node.js




```python
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}")
```



```javascript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.provod.ai/v1",
  apiKey: process.env.PROVOD_API_KEY,
});

const stream = await 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 await (const chunk of stream) {
  const delta = chunk.choices?.[0]?.delta?.content;
  if (delta) process.stdout.write(delta);
  if (chunk.usage) console.log("\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"`.

```python
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`):

```json
{
  "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`:

```json
{
  "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) or `cache_read_input_tokens` (Anthropic shape) across two consecutive requests. Check whether the selected model publishes a separate cache price in the [model catalog](/en/docs/models) and inspect final settlement through [`GET /v1/generation`](/en/docs/generation-metadata).

## Troubleshooting


**The model ignores json_schema or returns 400**


Check `supported_parameters` for the selected model in `GET /v1/models`. If `structured_outputs` is not supported, use `response_format: { "type": "json_object" }` with explicit schema rules in the system prompt, or select a model that advertises `structured_outputs`.


**The SSE stream closed before [DONE]**


Do not automatically retry after receiving initial text chunks. Preserve the delivered portion, check captured spend in [Usage and costs](/en/docs/usage-costs), and make an explicit continuation decision.
