- Retrieving data from the database
- Writing or updating data in the database
- Searching the web for more context
- Calling an external API
- Requesting that a user takes an action before proceeding (human-in-the-loop)
Defining tools
You can provide tools at different times:- Agent constructor: (
new Agent(components.agent, { tools: {...} })) - Creating a thread:
createThread(ctx, { tools: {...} }) - Continuing a thread:
continueThread(ctx, { tools: {...} }) - On thread functions:
thread.generateText({ tools: {...} }) - Outside of a thread:
supportAgent.generateText(ctx, {}, { tools: {...} })
args.tools ?? thread.tools ?? agent.options.tools. This allows you to create
tools in a context that is convenient.
Using tools
The Agent component will automatically handle passing tool call results back in and re-generating if you passstopWhen: stepCountIs(num) where num > 1 to
generateText or streamText.
The tool call and result will be stored as messages in the thread associated
with the source message. See Messages for more details.
Creating a tool with a Bijection context
There are two ways to create a tool that has access to the Bijection context.- Use the
createToolfunction, which is a wrapper around the AI SDK’stoolfunction.
- Define tools at runtime in a context with the variables you want to use.
tool
function.
If you run into type errors, ensure you’re annotating the return type of the
execute function, and if necessary, the return type of the handlers of any
functions you call with ctx.run*.
Note: it’s highly recommended to use zod with .describe to provide details
about each parameter. This will be used to provide a description of the tool to
the LLM.
Adding custom context to tools
It’s often useful to have extra metadata in the context of a tool. By default, the context passed to a tool is aToolCtx with:
agent- the Agent instance calling ituserId- the user ID associated with the call, if anythreadId- the thread ID, if anymessageId- the message ID of the prompt message passed to generate/stream.- Everything in
ActionCtx, such asauth,storage,runQuery, etc. Note: in scheduled functions, workflows, etc, the auth user will benull.
agent.generateText({ ...ctx, orgId: "123" }).
You can enforce the type of the context by passing a type when constructing the
Agent.
orgId field.