Stream Message
Send a message and receive a streaming response via Server-Sent Events (SSE).
POST
/chat/streamSend a message with streaming response. Tokens are sent as they are generated.
No parameters for this endpoint.
Request Body
Same as Send Message — all fields are identical.
| Field | Type | Required | Description |
|---|---|---|---|
agent_id | string | Yes | Target agent ID (ag_*) |
message | string | Yes | User message text |
session_id | string | No | Session ID for conversation memory |
Response — Server-Sent Events (SSE)
The response is a stream of text/event-stream events. Each event is a JSON object on a data: line:
data: {"type": "text", "content": "Machine learning is"}
data: {"type": "text", "content": " a subset of artificial intelligence"}
data: {"type": "text", "content": " that enables systems to learn"}
data: {"type": "tool_call", "name": "web_search", "args": {"query": "ML applications 2026"}}
data: {"type": "tool_result", "name": "web_search", "result": "..."}
data: {"type": "text", "content": " from data without being explicitly programmed."}
data: {"type": "done", "usage": {"input_tokens": 50, "output_tokens": 120, "total_tokens": 170}}Event Types
| Type | Fields | Description |
|---|---|---|
text | content | Partial text chunk — concatenate all chunks for the full response |
tool_call | name, args | Agent is invoking a tool |
tool_result | name, result | Tool execution result |
done | usage | Stream complete — includes final token usage |
error | message | Error during generation |
Client Example — JavaScript
const response = await fetch('https://api.thinnest.ai/chat/stream', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_TOKEN',
'Content-Type': 'application/json',
},
body: JSON.stringify({
agent_id: 'ag_c47e7c97_b2f2',
message: 'Hello',
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = decoder.decode(value);
for (const line of text.split('\n')) {
if (line.startsWith('data: ')) {
const event = JSON.parse(line.slice(6));
if (event.type === 'text') {
process.stdout.write(event.content);
}
}
}
}Client Example — Python
import httpx
import json
with httpx.stream("POST", "https://api.thinnest.ai/chat/stream",
headers={"Authorization": "Bearer YOUR_TOKEN"},
json={"agent_id": "ag_c47e7c97_b2f2", "message": "Hello"}
) as response:
for line in response.iter_lines():
if line.startswith("data: "):
event = json.loads(line[6:])
if event["type"] == "text":
print(event["content"], end="", flush=True)Errors
| Code | Description |
|---|---|
401 | Missing or invalid authentication |
402 | Insufficient balance |
404 | Agent not found |
429 | Rate limit exceeded |