Core concepts
Streaming
Set stream: true on /infer to receive the completion incrementally over Server-Sent Events (SSE) — ideal for a live typing effect.
Requesting a stream
bash
curl -N https://api.tnsaai.com/infer \
-H "x-api-key: $TNSA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "NGen-4.3-Pro",
"prompt": "Write a haiku about the sea.",
"stream": true
}'The response is sent as text/event-stream. Each line is a data: event terminated by [DONE].
Event payloads
Role init
The first event announces the assistant role.
json
data: {"role": "assistant"}Token deltas
Each subsequent event carries the next chunk of text.
json
data: {"token": "Hello"}
data: {"token": " world"}Thinking tokens
Reasoning models emit interim reasoning events when thinking is enabled.
json
data: {"type": "thinking_token", "content": "Checking base calculations", "iteration": 1}Tool-call notifications
json
data: {"type": "tool_call", "tool": "search_internet", "query": "Apple stock price"}Final stats
Just before completion, a stats event reports token usage, cost and latency.
json
data: {"stats": {"prompt_tokens": 14, "completion_tokens": 230, "cost": 0.00045, "latency": "1.8"}}Done
text
data: [DONE]Consuming the stream
python
import requests, json
with requests.post(
"https://api.tnsaai.com/infer",
headers={"x-api-key": "YOUR_API_KEY"},
json={"model": "NGen-4.3-Pro", "prompt": "Tell me a story.", "stream": True},
stream=True,
) as r:
for line in r.iter_lines():
if not line:
continue
payload = line.decode().removeprefix("data: ")
if payload == "[DONE]":
break
event = json.loads(payload)
if "token" in event:
print(event["token"], end="", flush=True)Cancelling a stream
Close the connection to stop generation
To interrupt a response, simply close the HTTP connection. The server detects the closed socket and halts model execution, so you stop being billed for further tokens.