Agents combine models with tools, autonomously reasoning and acting in a loop until the task is complete.
Agents are the most powerful abstraction in deepseek-kit. They combine models, tools, and multi-step loops — the model handles reasoning and decision-making, tools handle interaction with the external world, and the loop ensures continuous execution until a final answer is reached. This pattern is known as ReAct (Reasoning + Acting) and is the core paradigm for building AI applications.
Use createAgent() to create an agent. You only need to provide a model instance, and the agent is ready to work:
import { createAgent, createModel } from 'deepseek-kit'
const model = createModel({ model: 'deepseek-v4-flash' })
const agent = createAgent({ model })
const result = await agent.generate({
prompt: 'Hello!',
})
console.log(result.text)
But the real power of agents lies in tools. When equipped with tools, an agent can autonomously decide when to call tools, how to process results, and continue reasoning until the task is complete:
import { createAgent, createModel, tool } from 'deepseek-kit'
import { z } from 'zod'
const model = createModel({ model: 'deepseek-v4-flash' })
const weatherTool = tool({
name: 'getWeather',
description: 'Query weather information for a city',
schema: z.object({
city: z.string().describe('City name'),
}),
execute: async (input) => {
return `${input.city}: Sunny today, 22°C, humidity 60%.`
},
})
const agent = createAgent({
model,
tools: [weatherTool],
})
const result = await agent.generate({
prompt: 'How\'s the weather in Chongqing today?',
})
console.log(result.text)
Use the system parameter to set the agent's role and behavioral guidelines, guiding it to respond in a specific way:
const agent = createAgent({
model,
tools: [searchTool],
system: 'You are a research assistant. Always cite sources and provide detailed explanations.',
})
Use the fewShot parameter to provide example conversations that guide the model's response style and format. Few-shot examples are inserted after the system prompt and before the actual conversation, helping the model learn the desired pattern from demonstrations:
Agents can return structured data conforming to a Zod Schema instead of free-form text. This is particularly useful when you need to integrate the agent's output into other systems:
const agent = createAgent({
model,
tools: [weatherTool],
output: {
schema: z.object({
city: z.string(),
weather: z.string(),
temperature: z.number(),
recommendation: z.string(),
}),
},
})
const result = await agent.generate({
prompt: 'How\'s the weather in Beijing? Do I need an umbrella?',
})
console.log(result.output)
// { city: 'Beijing', weather: 'Sunny', temperature: 22, recommendation: 'No umbrella needed.' }
Agents support three lifecycle hooks that let you insert custom logic before and after each step — for logging, dynamically modifying messages, adjusting configuration, and handling errors:
Model instance, serving as the agent's reasoning engine.
toolsTool[]
List of available tools. The agent autonomously decides which tools to call based on user input.
systemstring
System prompt for setting the agent's role and behavioral guidelines.
fewShotChatMessage[]
Few-shot example messages, inserted after the system prompt and before the conversation. Used to guide the model's response style and format through demonstrations.
output{ schema: ZodSchema }
Structured output schema. When specified, the agent returns structured data conforming to the schema.
hooksGenerateTextHooks
Lifecycle hooks, including beforeStep, afterStep, and onError.
maxStepsnumber
50
Maximum number of execution steps. The loop terminates when this limit is reached.
signalAbortSignal
Abort signal for canceling an in-progress agent request.