Agent
The building blocks of an agentic app
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.
Creating an agent
Here’s a simple example of creating an agent in AXAR AI. This agent greets the user by their name.
What just happened?
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.
Dynamic system prompts
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:
Structured I/O
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.
Running agents
AXAR AI provides two methods for running agents:
agent.run()
The run()
method is an asynchronous function that processes the input and returns the complete output in a single response.
agent.stream()
The stream()
method provides a streaming interface that yields partial results as they become available, useful for real-time UI updates or processing long responses.
Both methods support the full range of AXAR AI features including:
Input/output schema validation
Tool execution
Telemetry and monitoring
Error handling
Model configuration
Last updated