Agents
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.
Creating an Agent
Use createAgent() to create an agent. You only need to provide a model instance, and the agent is ready to work:
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:
How Agents Work
Agents follow the ReAct loop:
- Reason — The model analyzes the user input and current context, deciding the next action
- Act — If a tool call is needed, the model generates a tool call; otherwise, it generates a text response directly
- Observe — Tool execution results are added to the conversation as input for the next step
- Loop — Repeat the above steps until the model generates a final response or the maximum number of steps is reached
The maximum number of steps defaults to 50. You can control this via maxSteps when creating the agent:
Streaming
Use the stream() method to receive the agent's output in real time, including text deltas and tool call events:
System Prompt
Use the system parameter to set the agent's role and behavioral guidelines, guiding it to respond in a specific way:
Few-Shot
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:
The message order in the final conversation is: system → fewShot → messages → prompt.
Structured Output
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:
Context Compaction
When agents run for many steps with tools, the conversation history grows continuously and can eventually exceed the model's context window. The compact option automatically summarizes older conversation rounds when the context usage approaches the limit, preserving the system prompt, few-shot examples, and recent rounds while compressing history into a concise summary.
Basic Usage
Enable compaction with default settings (threshold: 85% of context window, keep recent 3 rounds, model: deepseek-v4-flash):
Custom Configuration
Pass an object to customize compaction behavior:
threshold(number, default 0.85) — Context usage ratio that triggers compaction. Whenprompt_tokens >= contextWindowSize * threshold, history is summarized.keepRecentRounds(number, default 3) — Number of recent conversation rounds to preserve without summarization.model(Model, default'deepseek-v4-flash') — The LLM used to generate summaries.contextWindowSize(number, default 1,000,000) — The model's context window size in tokens.
How It Works
- At the start of each step, the agent checks if the previous step's
prompt_tokensexceeded the threshold - If triggered, older conversation rounds are summarized into a single message via LLM
- System prompts, few-shot examples, and the most recent rounds are always preserved
- The summary is inserted as a
usermessage withname: 'compact-summary'
Compaction runs before the model is called, ensuring the context is reduced before incurring the cost of a large prompt. If summarization fails for any reason, the original messages are preserved — the agent loop is never interrupted.
Lifecycle Hooks
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:
You can also terminate the loop early via hookCtx.stop():
You can also modify the current step's configuration by returning a config object in beforeStep:
This will cause the agent to switch to the Pro model after step 6.
API Reference
Parameters
true, older conversation rounds are automatically summarized when context usage approaches the limit. Pass an object { threshold?: number, keepRecentRounds?: number, model?: Model, contextWindowSize?: number } for custom settings. beforeStep, afterStep, and onError. 50Methods
params contains prompt or messages. text-delta, reasoning-delta, tool-call, step, and finish. StreamEvent Types
textDelta field. reasoningDelta field (available when thinking mode is enabled). step and toolCalls fields. step field. text and usage fields. 
