Agent Module#
The agent module provides the core Agent class for creating AI agents.
Agent Class#
- class pantheon.agent.Agent(name: str, instructions: str, model: str | list[str] | None = None, model_params: dict | None = None, icon: str = 'π€', tools: list[Callable] | None = None, response_format: Any | None = None, use_memory: bool = True, memory: Memory | None = None, tool_timeout: int | None = None, relaxed_schema: bool = False, max_tool_content_length: int | None = None, description: str | None = None)[source][source]#
Bases:
objectThe Agent class is the core component of Pantheon, providing a flexible interface for creating AI-powered agents with tools, memory, and collaboration capabilities.
- Parameters:
name β The name of the agent.
instructions β The instructions for the agent. The instructions are the system instructions that the agent will follow. All prompt composition (work strategy, delegation, skills, etc.) should be done at template parsing time, not at runtime.
model β The model to use for the agent. Can be a single model or list of fallback models.
model_params β The additional parameters for the model(LLM).
icon β The icon to use for the agent.
tools β The tools to use for the agent.
response_format β The response format to use for the agent. It can be a Pydantic model or a function that returns a Pydantic model.
use_memory β Whether to use memory for the agent. (default: True)
memory β The memory to use for the agent. If not provided, a new memory will be created.
tool_timeout β The timeout for the tool. (default: from settings.endpoint.local_toolset_timeout, or 3600s)
relaxed_schema β Use relaxed (non-strict) tool schema mode. (default: False)
max_tool_content_length β The maximum length of the tool content. (default: 100000)
description β The description of the agent. (default: None)
- __init__(name: str, instructions: str, model: str | list[str] | None = None, model_params: dict | None = None, icon: str = 'π€', tools: list[Callable] | None = None, response_format: Any | None = None, use_memory: bool = True, memory: Memory | None = None, tool_timeout: int | None = None, relaxed_schema: bool = False, max_tool_content_length: int | None = None, description: str | None = None)[source][source]#
- property tool_timeout: int[source]#
Public API property for backward compatibility and dynamic access.
- property max_tool_content_length: int[source]#
Public API property for backward compatibility and dynamic access.
- tool(func: Callable, key: str | None = None)[source][source]#
Add a tool to the agent.
- Parameters:
func β The tool function to add to the agent.
key β The key to use for the tool. If not provided, the name of the function will be used.
- Returns:
The agent instance.
- remove_tool(key: str) bool[source][source]#
Remove a tool from the agent.
- Parameters:
key β The name of the tool to remove.
- Returns:
True if the tool was found and removed, False otherwise.
- Return type:
- async toolset(toolset: ToolSet | ToolProvider) Agent[source][source]#
Add a toolset to the agent (supports both ToolSet and ToolProvider)
Behavior: - ToolSet: Automatically wrapped in LocalProvider for unified provider-based routing - ToolProvider (LocalProvider/ToolSetProvider): Dynamic routing - tools retrieved on-demand
- Parameters:
toolset β Either a ToolSet instance (will be wrapped in LocalProvider) or a ToolProvider instance (used directly)
- Returns:
The agent instance
- async mcp(name: str, provider: ToolProvider) Agent[source][source]#
Add a MCP provider to the agent (one at a time)
Dynamic routing approach: Provider tools are retrieved on-demand via get_tools_for_llm() and called via call_tool() with prefix routing. No pre-wrapping.
- Parameters:
name β Name/identifier for this provider (e.g., βbiomcpβ)
provider β ToolProvider instance (MCPProvider recommended, already initialized)
- Returns:
The agent instance
- async get_tools_for_llm() list[dict][source][source]#
Get all tools for LLM - includes _base_functions and provider tools
Dynamically retrieves tools from providers (utilizing their caching mechanisms). Provider tools are prefixed with {provider_name}_ to distinguish them from agentβs own tools.
- Returns:
List of tool definitions in OpenAI format
- async call_tool(prefixed_name: str, args: dict, context_variables: dict | None = None, tool_call_id: str | None = None) Any[source][source]#
Call a tool by name with automatic routing and suffix matching fallback.
- async run(msg: str | BaseModel | AgentResponse | list[str | BaseModel | dict] | AgentTransfer | VisionInput, response_format: Any | None = None, tool_use: bool = True, context_variables: dict | None = None, process_chunk: Callable | None = None, process_step_message: Callable | None = None, check_stop: Callable | None = None, memory: Memory | None = None, use_memory: bool | None = None, update_memory: bool = True, tool_timeout: int | None = None, model: str | list[str] | None = None, allow_transfer: bool = True, execution_context_id: str | None = None) AgentResponse | AgentTransfer[source][source]#
Run the agent.
- Parameters:
msg β The input message to the agent.
response_format β The response format to use.
tool_use β Whether to use tools.
context_variables β Runtime variables available to tools during execution. These are stored in ExecutionContext.runtime_context and injected into tools that request them via the βcontext_variablesβ parameter. Example: {βuser_idβ: β123β, βsession_idβ: βabcβ}
process_chunk β The function to process the chunk.
process_step_message β The function to process the step message.
check_stop β The function to check if the agent should stop.
memory β The memory to use.
use_memory β Whether to use short term memory.
update_memory β Whether to update the short term memory.
tool_timeout β The timeout for the tool.
model β The model to use in this run. Could be a list of models for fallback. If not provided, the model will be selected from the agentβs models.
allow_transfer β Whether to allow transfer to another agent.
execution_context_id β Unique identifier for sub-agent delegation contexts. Used for message filtering in _run_stream to isolate messages by delegation context. None for primary/team agents.
- Returns:
The agent response. Either an AgentResponse or an AgentTransfer. If the agent is interrupted, the AgentResponse will have the interrupt flag set to True. If the agent is transferring to another agent, the AgentTransfer will be returned.
- async run_loop(process_chunk: Callable | None = None, process_step_message: Callable | None = None, check_stop: Callable | None = None, context_variables: dict | None = None, memory: Memory | None = None, tool_timeout: int | None = None, model: str | list[str] | None = None, on_response: Callable | None = None, on_error: Callable | None = None) None[source][source]#
Persistent event-driven loop consuming messages from input_queue.
Blocks until stop_loop() is called or the task is cancelled. Each message dequeued triggers a full agent.run() cycle. Background task completions auto-enqueue notifications, so the agent is automatically triggered when bg tasks finish.
- Parameters:
process_chunk β Streaming chunk callback (forwarded to run()).
process_step_message β Step message callback (forwarded to run()).
check_stop β Stop check callback (forwarded to run()).
context_variables β Shared context variables for all runs.
memory β Memory instance to use.
tool_timeout β Tool timeout (forwarded to run()).
model β Model override (forwarded to run()).
on_response β Callback(AgentResponse) after each successful run.
on_error β Callback(Exception) when a run fails.
- setup_bg_notify_queue(queue: Queue)[source][source]#
Wire bg task completion to an external asyncio.Queue.
Alternative to run_loop() for frontends that manage their own event loops (e.g. REPL with prompt_toolkit, ChatRoom server).
- Parameters:
queue β The asyncio.Queue to push notification strings into.
Constructor Parameters#
Parameter |
Type |
Description |
|---|---|---|
name |
str |
Unique name for the agent |
instructions |
str |
System instructions defining agent behavior |
model |
str | list[str] |
LLM model(s) to use (default: βgpt-4.1-miniβ) |
icon |
str |
Display icon for the agent (default: βπ€β) |
tools |
list[Callable] |
List of tool functions the agent can use |
response_format |
Any |
Expected response format (for structured outputs) |
use_memory |
bool |
Enable memory persistence (default: True) |
memory |
Memory |
Custom memory instance |
tool_timeout |
int |
Tool execution timeout in seconds (default: 600) |
relaxed_schema |
bool |
Use relaxed (non-strict) tool schema mode (default: False) |
max_tool_content_length |
int | None |
Maximum length for tool outputs (default: 100000) |
Methods#
The Agent class provides the following key methods:
tool(func): Decorator to add a tool function to the agentchat(): Start an interactive chat sessionrun(messages): Run the agent with given messagesremote_toolset(service_id): Connect to a remote toolset
Response Classes#
- class pantheon.agent.AgentResponse(*, agent_name: str, content: Any, details: ResponseDetails | None, interrupt: bool = False)[source][source]#
Bases:
BaseModelThe AgentResponse class is used to store the agent response.
Type Definitions#
- pantheon.agent.AgentInput[source]#
Type alias for valid agent inputs:
str: Simple text messageBaseModel: Structured inputAgentResponse: Response from another agentlist[str | BaseModel | dict]: Multiple inputsAgentTransfer: Transfer from another agentVisionInput: Image input
alias of
str|BaseModel|AgentResponse|list[str|BaseModel|dict] |AgentTransfer|VisionInput
Examples#
Basic Agent#
from pantheon.agent import Agent
agent = Agent(
name="assistant",
instructions="You are a helpful AI assistant."
)
# Interactive chat
await agent.chat()
Agent with Tools#
from pantheon.agent import Agent
agent = Agent(
name="calculator",
instructions="You help with calculations."
)
@agent.tool
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
@agent.tool
def multiply(a: float, b: float) -> float:
"""Multiply two numbers."""
return a * b
# Run with tools
result = await agent.run("What is 5 + 3?")
print(result.content)
Agent with Memory#
from pantheon.agent import Agent
from pantheon.memory import Memory
# Custom memory
memory = Memory(agent_id="unique_id")
agent = Agent(
name="memory_bot",
instructions="Remember our conversations.",
memory=memory
)
# First interaction
await agent.run("My name is Alice")
# Later interaction (remembers context)
result = await agent.run("What's my name?")
print(result.content) # Should mention "Alice"
Multi-Model Fallback#
agent = Agent(
name="robust_agent",
instructions="Handle tasks reliably."
)
Remote Toolset#
from pantheon.toolsets.python import PythonInterpreterToolSet
from pantheon.toolsets.utils.toolset import run_toolsets
async def setup():
toolset = PythonInterpreterToolSet("python")
async with run_toolsets([toolset]):
agent = Agent(
name="coder",
instructions="You can run Python code."
)
await agent.remote_toolset(toolset.service_id)
result = await agent.run("Calculate fibonacci(10)")
print(result.content)