Tools are functions that extend the abilities of an Agent. By building tools, you can allow your agent to execute code, call out to external APIs or your own database, or turn unstructured inputs into structured responses. Working with AI presents a bit of a paradox for engineers. Software is highly deterministic; that is, given a specific input, you can expect software to consistently generate an expected output. AI models don’t behave that way. They often act in a nondeterministic — or if you prefer, creative — way. That’s what makes them interesting and powerful, but can also make them frustrating to work with. By adding tools, you can get the best of both worlds, by mixing the creativity of AI models with the structure and clarity of executable code.

Your first tool

An example of this behavior is something that surprises a lot of people when they start experimenting with AI. If you ask a model the question “how many Rs are in the word strawberry?”, it will often struggle to get the answer correct. To solve that, we can instead create an agent with a simple tool:
import { Tool } from '@ardent-ai/sdk';
import { z } from 'zod';

const countLetters = new Tool({
  name: 'countLetters',
  description: 'Count the number of times a given letter appears in a phrase',
  parameters: {
    letter: z.string(),
    phrase: z.string(),
  },
  result: z.number(),
  async execute({ phrase, letter }) {
    const regex = new RegExp(letter.toLowerCase(), 'g');
    const matches = phrase.toLowerCase(regex).match();
    return matches ? matches.length : 0;
  }
});
Obviously, this is a silly example, but keep in mind that tools are arbitrary executable code. They can be whatever you can dream up.

See also

  • Tool in the SDK reference