Agent
The building blocks of an agentic app
Last updated
The building blocks of an agentic app
Last updated
Agents in AXAR AI are the primary building blocks. They represent a tightly coupled set of tasks. Agents can work independently in a developer-defined workflow or collaborate to build more complex autonomous workflows.
Conceptually, an agent in AXAR AI is a container for:
LLM model: The LLM the agent uses.
System prompts: Instructions to guide the LLM.
Structured I/O: Data structures defining the expected input and output of the agent.
Dependencies: External objects passed to the agent, available across prompts, tools, and validators within the agent.
Tools: Methods the agent can call to gather information or perform actions as needed.
Here’s a simple example of creating an agent in AXAR AI. This agent greets the user by their name.
Model
The @model
decorator sets the AI model to use. Here, it’s using openai:gpt-4o-mini
.
Prompt
The @systemPrompt
decorator defines the agent’s behavior. In this case, the agent greets users by their name in a friendly way.
Agent
The GreetingAgent
class extends Agent
, taking a string
as input and returning a string
as output.
Run
The agent runs with the input "My name is Alice."
and generates a friendly greeting based on the system prompt.
In the previous example, the run()
method assumes the user's name is included in the input. But what if the input doesn’t contain the name?
We can handle this by modifying the agent to accept the user's name as a parameter in its constructor()
. We can then generate a dynamic system prompt using the provided name. This is done by combining the @systemPrompt
annotation with an instance method inside the agent class.
With this setup, the username is passed during instantiation every time and also doesn't need to be included in every run()
call.
Dynamic system prompts let us change an agent's behavior in runtime based on external context. We can pass that context to the agent through its constructor()
as a dependency, or the method defining the dynamic prompt can fetch the external context directly.
Here’s how it works:
The above example also shows how to inject external dependencies to an agent. Anything passed into the agent's constructor()
can be accessed within the agent throughout its lifetime.
So far, we’ve seen how to pass a string to an agent and get a string response. However, in many cases, we might want our agent to handle structured input and output for better control, validation, and clarity. AXAR makes this possible with schemas, allowing us to define structured data formats for both input and output.
With structured I/O, we can:
Validate input data before passing it to the agent.
Ensure the output conforms to a predefined structure and validation rules.
Handle complex use cases with multiple fields and specific data types.
The following example demonstrates a GreetingAgent
that takes structured input, including the user's name, mood, day of the week, and language preference. The agent then generates structured output with a greeting, a response to the user's mood, and an optional weekend message.
AXAR AI provides two methods for running agents:
agent.run()
: An async function that returns the result of the run.
agent.runStream()
: Streams the response as an async iterable. <🚧 WIP>