Overview
Tinkuy supports streaming responses via agent.stream(). The agent sends tokens, tool call events, and completion events as they happen.
Usage
const agent = new Agent({
router,
tools: [myTool],
systemPrompt: 'You are a helpful assistant.',
});
const stream = agent.stream('Tell me a story about AI');
for await (const chunk of stream) {
switch (chunk.type) {
case 'token':
process.stdout.write(chunk.data);
break;
case 'tool_call':
console.log('\n[Tool called:', chunk.data.tool, ']');
break;
case 'tool_result':
console.log('\n[Tool result:', chunk.data, ']');
break;
case 'done':
console.log('\n[Done]', chunk.data);
break;
}
}
Stream Events
| Event | Data | Description |
|---|---|---|
token |
string |
A text token from the LLM |
tool_call |
{ tool, args } |
The LLM invoked a tool |
tool_result |
any |
The tool’s execution result |
done |
AgentResult |
Agent completed with final result |
With React (frontend)
const response = await fetch('/api/agent', {
method: 'POST',
body: JSON.stringify({ prompt: '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);
const lines = text.split('\n').filter(Boolean);
for (const line of lines) {
const chunk = JSON.parse(line.replace(/^data: /, ''));
if (chunk.type === 'token') {
// Append token to UI
}
}
}
On Complete Hook
const agent = new Agent({
router,
tools: [myTool],
systemPrompt: '...',
onComplete: (result) => {
console.log('Agent finished:', {
iterations: result.iterations,
tools: result.toolsUsed,
cost: result.costUsd,
latency: result.latencyMs,
});
},
});