Agent
Core AI Agent class for executing tasks and tools
The Agent
class is the central component of the xpander.ai SDK, representing an AI agent capable of executing tasks, using tools, and maintaining conversational state.
Overview
An Agent has several key capabilities:
- Running tasks and managing execution flow
- Executing tools and function calls
- Managing conversation memory and state
- Building and navigating graph-based workflows
- Integrating with external services through agentic interfaces
All agent instances are retrieved from the XpanderClient. You typically won’t create Agent instances directly.
Retrieving an Agent
Core Properties
Property | Type | Description | Availability |
---|---|---|---|
id | string | Unique identifier for the agent | Always available |
name | string | Name of the agent | Always available |
instructions | string | System instructions for the agent | Always available |
metadata | object | Contains metadata like description | Always available |
execution | Execution | Current execution context | After adding a task |
memory | Memory | Memory management interface | Always available |
messages | List[Dict] | Conversation messages | After memory initialization |
graph | Graph | Workflow graph system | Always available |
tool_choice | string | Setting for tool selection behavior | Always available |
Some properties require initialization before they can be accessed. For example, the execution
property is only available after calling add_task()
(Python) or addTask()
(TypeScript), and messages
is only available after initializing memory with memory.init_messages()
(Python) or memory.initMessages()
(TypeScript).
Initialization Sequence
Most agents follow this initialization sequence:
Get an agent
Add a task
Initialize memory
Run the agent loop
Task Management Methods
add_task() / addTask()
Adds a new task for the agent to process.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
input | string | Yes | Task description or user query |
files | List[Dict] | No | Files to include with the task |
use_worker | boolean | No | Whether to use async worker (default: True) |
thread_id | string | No | Thread ID for conversation continuity |
Returns
Returns an Execution
object representing the new task.
is_finished() / isFinished()
Checks if the current task execution is finished.
Returns
Returns a boolean: True
if the execution is completed, False
otherwise.
For multi-agent workflows, you can enforce transitions to the next agent without checking is_finished()
. This allows you to implement custom transition logic based on your specific requirements rather than relying on the agent’s built-in completion status. See Enforcing Agent Transitions Without Manager for details on implementing this advanced pattern.
retrieve_execution_result() / retrieveExecutionResult()
Gets the result of the current execution.
Returns
Returns an ExecutionResult
object containing status and result content.
Memory Management Methods
memory.init_messages() / memory.initMessages()
Initializes the agent’s memory with the task input and instructions.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
input | string | Yes | Input message to initialize with |
instructions | string | Yes | System instructions for the agent |
llm_provider | LLMProvider | No | The LLM provider type (default: LLMProvider.OPEN_AI) |
add_messages() / addMessages()
Adds messages to the agent’s conversation memory.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
messages | Dict | Yes | Messages to add (e.g., LLM response) |
Complete Example
Here’s a complete example of using an Agent to execute a task: