Tools
Tools enable models to interact with the external world — querying databases, calling APIs, performing calculations — breaking through the limitations of training data.
Tools are the bridge between models and the external world in deepseek-kit. Models can only generate text based on training data, but through tools, they can query real-time weather, search databases, execute code — anything you can implement in JavaScript can be encapsulated as a tool for the model to use. When the model determines that a tool call is needed, it generates a tool call request; your code executes the tool and returns the result, and the model continues reasoning based on the result.
Defining Tools
Use the tool() function to define a tool. Each tool consists of four core parts: name, description, parameter schema, and execute function:
name— Unique identifier for the tool. The model uses the name to choose which tool to calldescription— Functional description of the tool, helping the model understand when it should call this toolschema— Parameter schema defined with Zod, used both to describe the parameter structure to the model and to validate the parameters generated by the modelexecute— Async execution function that receives schema-validated parameters and returns the tool execution result
Using Tools in Agents
Pass tools to createAgent, and the agent will autonomously decide when to call tools based on user input:
You can also use tools directly with generateText:
Strict Mode
When strict mode is enabled, deepseek-kit enforces parameter structure constraints at the JSON Schema level — setting additionalProperties: false and marking all properties as required. This improves the reliability of model-generated parameters:
Strict mode modifies the generated JSON Schema to ensure the model doesn't generate fields outside the schema. However, not all models support strict mode — unsupported models will ignore this option.
Required Tools
Setting required: true forces the model to call the tool instead of freely choosing whether to call it. This is useful when you need to ensure the model always performs a specific operation (e.g., data validation, permission checks):
When only one tool is marked as required, tool_choice is set to that tool; when multiple tools are marked as required, tool_choice is set to "required".
Multiple Tools
You can pass multiple tools simultaneously, and the model will autonomously choose which tools to call based on context:
Tool Execution Results
Tool execution results are automatically serialized as strings and returned to the model. The tool() function internally handles result wrapping:
- On success: Returns
{ success: true, data: <result> } - On failure: Returns
{ success: false, error: "<error message>" }
Your execute function can return any type — string, object, number — deepseek-kit handles serialization automatically:
Timeout and Retry
Timeout
Set a timeout for tools that may take a long time to avoid waiting indefinitely. The default timeout is 60 seconds:
Retry
Configure automatic retry count for unreliable tools. When execution fails, deepseek-kit automatically retries until success or the maximum retry count is reached:
Timeout and retry can be combined — each retry is subject to the timeout limit:
Result Compaction
When tools return large outputs (e.g. file contents, API responses), the raw result can consume a significant portion of the model's context window. The compact option uses an LLM to compress verbose tool results while preserving all information the agent needs to continue reasoning.
Basic Usage
Enable compaction with default settings (threshold: 1500 characters, model: deepseek-v4-flash):
Custom Configuration
You can customize the compaction behavior by passing an object instead of true:
threshold(number, default 1500) — Minimum character length of a tool result before compaction is triggered. Results shorter than this are returned as-is.model(Model, default'deepseek-v4-flash') — The LLM used to compress content.
Compaction results are cached in memory. When the same tool is called repeatedly with identical arguments, the compacted result is served from cache instead of making another LLM call. If compaction fails for any reason, the original uncompressed result is returned — tool execution is never affected.
AbortSignal
deepseek-kit supports AbortSignal throughout the tool execution pipeline, allowing you to cancel in-progress tool calls. This is useful when users cancel requests or when you need to enforce time limits at the application level.
Usage with Agents
Pass a signal to generateText or agent.generate to enable cancellation:
When the signal is aborted:
- In-progress tool executions are cancelled immediately
- Pending retries are skipped
- The agent loop stops and throws an
AbortError
Integration with Timeout
AbortSignal works alongside the timeout option. The timeout acts as a per-attempt time limit, while AbortSignal provides external cancellation:
Parameter Validation
The tool() function automatically uses the Zod Schema to validate model-generated parameters. If parameters don't conform to the schema, the validation error is caught and returned to the model as a failure result, rather than throwing an exception that interrupts the flow:
If the model generates limit: -1, Zod validation will fail, and the model will receive an error message like "Invalid arguments: limit: Number must be greater than or equal to 1", giving it the opportunity to correct the parameters and retry.
Error Handling
Errors during tool execution are automatically caught and returned to the model. You can handle errors yourself in the execute function, or let deepseek-kit's default mechanism take over:
Even if your execute function throws an uncaught exception, deepseek-kit will catch it and convert it to a { success: false, error: "..." } format result returned to the model, ensuring the flow is never interrupted.
Tool Calling in Streaming
When using the stream() method, tool call events are pushed in real time as part of the stream events:
API Reference
tool() Parameters
falseadditionalProperties: false and mark all properties as required. false600000undefinedtrue, tool results exceeding the threshold are compressed via an LLM to save context window space. Pass an object { threshold?: number, model?: Model } for custom settings. Tool Type
tool() function. Contains the original configuration properties and processed parameters (JSON Schema) and execute (wrapped execution function). Tool Result Types
data field contains the return value of the execute function. error field contains the error description. ToolChoice Type
required: true. 
