Agents in TypeScript, typed from tool to stream
Kikoyu is a small SDK for backend engineers. Define tools with Zod, hand off between agents, keep session memory, and stream every step of a run as a typed event.
const refund = createTool({ name: 'process_refund', description: 'Issue a refund for an invoice', parameters: z.object({ invoiceId: z.string() }), requiresApproval: true, execute: ({ invoiceId }) => refunds.issue(invoiceId),});
const billing = Agent.builder() .setName('Billing Specialist') .addTool(refund) .build();
const triage = Agent.builder() .setName('Triage Router') .withHandoffs(billing) .build();
const events = runner.stream(triage, input, { approvalHandler: async () => true,});input
I need a refund for invoice #INV-9921
- Typed stream events
- 9
- One union, from text_delta to run_complete
- Model providers
- 3
- OpenAI, Anthropic and Gemini, chained with FallbackProvider
- Memory stores
- 4
- In-memory, file, Redis and Mem0
- Vitest cases
- 38
- Across core and the HTTP API package
Six primitives and one Runner
Each one is a documented export of @kikoyu/core. Use the ones a run needs and leave the rest out.
createTool()Write the schema once
Tool parameters are a Zod schema. Kikoyu turns it into the JSON Schema the model sees, then checks the model’s arguments against the same schema before execute runs.
parameters: z.object({ invoiceId: z.string().regex(/^INV-\d{4}$/), reason: z.enum(['duplicate', 'damaged', 'not_received']),}),{ "type": "object", "properties": { "invoiceId": { "type": "string", "pattern": "^INV-\\d{4}$" }, "reason": { "type": "string", "enum": ["duplicate", "damaged", "not_received"] } }, "required": ["invoiceId", "reason"], "additionalProperties": false}Handoffs
Route a run to a specialist agent. The result tells you which agent finished it.
createHandoff()Sessions
Keep turn history across runner.run() calls, in memory, on disk, in Redis or in Mem0.
new Session()Streaming
Read text, tool calls, handoffs and approvals as they happen, as one typed union.
runner.stream()Guardrails
Input checks run before the first model call and stop the run before any tool executes.
createGuardrail()Tracing
Collect a RunTrace with a timed step for every model call, tool call, handoff and guardrail.
TraceCollectorStructured output
Validate the final answer against a Zod schema, with a repair pass when the model drifts.
withResponseSchema()
From install to a first run in three steps
The quickstart uses the same calls as the tests in packages/core/test, so the snippets you copy are the ones under test.
- 01
Install the package
Zod is a peer dependency, so your schemas stay in your own codebase.
$ pnpm add @kikoyu/core zod - 02
Build an agent
Name, instructions, tools and model settings all live on one builder.
const agent = Agent.builder() .setName('Assistant') .setInstructions('Be brief.') .addTool(getStockPrice) .build(); - 03
Run it
runner.run() returns the final text and the agent that wrote it.
const runner = new Runner();const result = await runner.run( agent, 'What is AAPL trading at?',); console.log(result.text);
How a run moves through the SDK
Each stage names the events it can emit, so whatever reads the stream knows what to expect and when.
Input
Your app passes a message to the runner, with an optional session and context. Input guardrails run here, one after another, before the first model call.
Call
runner.stream(agent, input, { session, context })Events
- guardrail_triggered
- run_failed
Only when a guardrail blocks the input
Patterns from the docs
Each pattern has a guide in the docs and a runnable script in the examples package.
Single agent with tools
A financial agent with Zod-typed tools for stock price and trading volume.
Read the guideconst result = await runner.run( financialAgent, 'What is the current stock price and trading volume of AAPL?',);Multi-agent handoff
A triage router that hands coding questions to a code specialist and math to a math specialist.
Read the guideconst result = await runner.run( triageRouter, 'Write a TypeScript function that checks for a palindrome',); result.agent.name; // 'Code Specialist'Session memory
Two turns that share one session, so the second answer remembers the first.
Read the guideconst session = new Session({ sessionId: 'session_usr_1042', store }); await runner.run(agent, 'My usual order is an iced oat latte.', { session });await runner.run(agent, 'What should I order today?', { session });for await (const event of runner.stream(agent, input)) { if (event.type === 'text_delta') process.stdout.write(event.delta); if (event.type === 'tool_call_started') log(event.toolCall.name);}
Read the source before you adopt it
Kikoyu is MIT licensed and published on npm. The API reference is generated from the TypeScript source, so the docs list exactly what the package exports.
- Package
- @kikoyu/core
- Version
- 0.1.13
- License
- MIT
- Modules
- ESM and CommonJS
- Tests
- 38 Vitest cases
Found something the docs get wrong? Open an issue
