Create a basic agent
We want to use await
so we're going to wrap all of our code in a main
function, like this:
For the rest of this guide we'll assume your code is wrapped like this so we can use await
. You can run the code this way:
Load your dependencies
First we'll need to pull in our dependencies. These are:
- The OpenAI class to use the OpenAI LLM
- tool to provide tools to our agent
- agent to create the single agent
- Settings to define some global settings for the library
- Dotenv to load our API key from the .env file
- Zod to define the schema for our tool
Initialize your LLM
We need to tell our OpenAI class where its API key is, and which of OpenAI's models to use. We'll be using gpt-4o
, which is capable while still being pretty cheap. This is a global setting, so anywhere an LLM is needed will use the same model.
Create a function
We're going to create a very simple function that adds two numbers together. This will be the tool we ask our agent to use.
Note that we're passing in an object with two named parameters, a
and b
. This is a little unusual, but important for defining a tool that an LLM can use.
Turn the function into a tool for the agent
This is the most complicated part of creating an agent. We need to define a tool
. We have to pass in:
- The function itself (
sumNumbers
) - A name for the function, which the LLM will use to call it
- A description of the function. The LLM will read this description to figure out what the tool does, and if it needs to call it
- A schema for function. We tell the LLM that the parameter is an
object
, and we tell it about the two named parameters we gave it,a
andb
. We describe each parameter as anumber
, and we say that both are required. - You can see more examples of function schemas.
We then wrap up the tools into an array. We could provide lots of tools this way, but for this example we're just using the one.
Create the agent
With your LLM already set up and your tools defined, creating an agent is simple:
Ask the agent a question
We can use the chat
interface to ask our agent a question, and it will use the tools we've defined to find an answer.
You will see the following output:
Output
To stream the response, you can use the AgentStream
event which provides chunks of the response as they become available. This allows you to display the response incrementally rather than waiting for the full response:
Streaming Output
Logging workflow events
To log the workflow events, you can check the event type and log the event data.
Let's see what running this looks like using npx tsx agent.ts
Output
We're seeing several workflow events being logged:
AgentToolCall
- Shows the agent preparing to call our tool with the numbers 202 and 404AgentToolCallResult
- Shows the result of calling the tool, which returned "606"AgentInput
- Shows the original user inputAgentOutput
- Shows the agent's response
Great! We've built an agent that can understand requests and use tools to fulfill them. Next you can:
Last updated on