Running agents
Run, stream, continue a conversation, and read the result.
run(agent, input, options?) runs an agent until it answers, running its tools along the way.
import { run } from 'cencori/agents';
const result = await run(support, 'Where is order 1042?');Options
The result
Streaming
const stream = run(support, 'Where is order 1042?', { stream: true });
for await (const event of stream) {
switch (event.type) {
case 'text_delta': process.stdout.write(event.delta); break;
case 'tool_call': console.log('calling', event.toolName); break;
case 'handoff': console.log(`${event.from} → ${event.to}`); break;
}
}
const result = await stream.completed;stream.toTextStream() yields only the text.
Conversations
Each run lives in a Cencori session. Pass sessionId to continue it: the agent sees the earlier turns.
const first = await run(support, 'Where is order 1042?');
const next = await run(support, 'Can you cancel it?', { sessionId: first.sessionId });A session the user stops using expires on its own; idle sessions cost nothing.
Errors
All extend AgentsError.
import { CencoriRunError, MaxTurnsExceededError } from 'cencori/agents';
try {
await run(support, input);
} catch (error) {
if (error instanceof CencoriRunError && error.status === 402) {
// Out of credits
}
}
