Overview

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:

import { tool } from 'deepseek-kit'
import { z } from 'zod'

const weatherTool = tool({
  name: 'getWeather',
  description: 'Query weather information for a specified city',
  schema: z.object({
    city: z.string().describe('City name'),
  }),
  execute: async (input) => {
    return `${input.city}: Sunny today, 22°C, humidity 60%.`
  },
})
  • name — Unique identifier for the tool. The model uses the name to choose which tool to call
  • description — Functional description of the tool, helping the model understand when it should call this tool
  • schema — Parameter schema defined with Zod, used both to describe the parameter structure to the model and to validate the parameters generated by the model
  • execute — 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:

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 specified 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 Beijing today?',
})

console.log(result.text)

You can also use tools directly with generateText:

import { createModel, generateText, tool } from 'deepseek-kit'
import { z } from 'zod'

const model = createModel({ model: 'deepseek-v4-flash' })

const result = await generateText({
  model,
  tools: [weatherTool],
  messages: [{ role: 'user', content: 'How\'s the weather in Beijing today?' }],
})