Stream Message

Send a message and receive a streaming response via Server-Sent Events (SSE).

POST/chat/stream

Send 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.

FieldTypeRequiredDescription
agent_idstringYesTarget agent ID (ag_*)
messagestringYesUser message text
session_idstringNoSession 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

On this page