Skip to main content
POST
/
chat
/
stream
curl -X POST https://api.thinnest.ai/chat/stream \
  -H "Authorization: Bearer $THINNESTAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "agent_id": "ag_c47e7c97_b2f2",
  "message": "Explain how machine learning works",
  "session_id": "sess_customer_001"
}'
{
  "detail": [
    {
      "loc": [
        "<string>"
      ],
      "msg": "<string>",
      "type": "<string>"
    }
  ]
}
curl -X POST https://api.thinnest.ai/chat/stream \
  -H "Authorization: Bearer $THINNESTAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "agent_id": "ag_c47e7c97_b2f2",
  "message": "Explain how machine learning works",
  "session_id": "sess_customer_001"
}'

Request Body

Same as Send Message — all fields are identical.
agent_id
string
required
Target agent ID (ag_*)
message
string
required
User message text
session_id
string
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

TypeFieldsDescription
textcontentPartial text chunk — concatenate all chunks for the full response
tool_callname, argsAgent is invoking a tool
tool_resultname, resultTool execution result
doneusageStream complete — includes final token usage
errormessageError 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

CodeDescription
401Missing or invalid authentication
402Insufficient balance
404Agent not found
429Rate limit exceeded

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Body

application/json
agent_id
string
required
query
string
required
user_id
string | null
session_id
string | null

Response

Successful Response