Cookbook
Tools + memory together
The "chat with state and actions" loop — persistent memory plus tool execution.
Most real agent apps need both memory (recall past turns) and tools (perform actions). Here's the minimum viable wiring.
Verified·define-toolimport { useChat } from '@agentskit/react'
import { openai } from '@agentskit/adapters/openai'
import { defineTool } from '@agentskit/tools'
import { sqliteMemory } from '@agentskit/memory/sqlite'
const adapter = openai({ model: 'gpt-4o-mini' })
const getOrders = defineTool({
name: 'get_orders',
description: 'List orders for the current user',
schema: { userId: 'string' },
async execute({ userId }) {
return db.query('select * from orders where user_id = ?', [userId])
},
})
const memory = sqliteMemory({ path: './threads.sqlite' })
export function Support() {
const { messages, input, setInput, send } = useChat({
adapter,
memory,
tools: [getOrders],
threadId: 'user-123',
})
// …render as in the Streaming recipe
}Pitfall
Do not pass a fresh sqliteMemory(...) inline to useChat. Create the memory once at the module level — otherwise you'll open a new database handle on every render.
const memory = sqliteMemory({ path: './threads.sqlite' })
// …
useChat({ adapter, memory })useChat({
adapter,
memory: sqliteMemory({ path: './threads.sqlite' }), // new handle every render
})Explore nearby
- PeerCookbook
Copy-paste recipes for the things every agent app needs. Each recipe stands on its own.
- PeerStreaming chat
useChat + abort + back-pressure. The minimum viable streaming chat, production-ready.
- PeerAuth in tool calls
Scope tool execution to the current authenticated user. Never trust the model with who is calling.