Skip to content

Agents

The Agent class is the core primitive in Elsai. It wraps a language model, manages conversation history, executes tools, and coordinates the full reasoning loop.

Basic usage

python
from elsai import Agent

agent = Agent()
result = agent("Summarize the top trends in AI for 2025")
print(result)

Constructor parameters

ParameterTypeDefaultDescription
modelModel | str | NoneBedrockModel()The LLM to use. Pass a model instance, a Bedrock model ID string, or None for the default.
toolslist | NoneNoneTools available to the agent.
system_promptstr | list | NoneNoneInstructions that guide model behavior.
messageslist | None[]Pre-load conversation history.
conversation_managerConversationManager | NoneSlidingWindowConversationManagerControls how conversation history is managed.
callback_handlerCallable | NonePrintingCallbackHandlerReceives streaming events during execution.
structured_output_modeltype[BaseModel] | NoneNonePydantic model for structured responses.
session_managerSessionManager | NoneNonePersists conversation state across runs.
hookslist | NoneNoneLifecycle event hooks.
pluginslist | NoneNonePlugin instances to extend functionality.
agent_idstr | None"default"Identifier for this agent (used in sessions and multi-agent).
namestr | None"Elsai Agents"Human-readable agent name.
descriptionstr | NoneNoneWhat this agent does (used when wrapping as a tool).
stateAgentState | dict | NoneAgentState()Shared mutable state accessible to tools.
load_tools_from_directoryboolFalseAuto-load tools from ./tools/ with hot-reload.
tool_executorToolExecutor | NoneConcurrentToolExecutorControls how parallel tool calls are executed.
retry_strategyModelRetryStrategy | NoneDefault retryRetry behaviour on transient errors.

Calling the agent

Natural language input

python
result = agent("What is the capital of France?")

Multi-modal input (images, documents)

python
import base64

with open("chart.png", "rb") as f:
    image_data = base64.b64encode(f.read()).decode()

result = agent([
    {"image": {"format": "png", "source": {"bytes": image_data}}},
    {"text": "Describe what this chart shows."}
])

Passing full messages

python
result = agent([
    {"role": "user", "content": [{"text": "Hello"}]},
    {"role": "assistant", "content": [{"text": "Hi there! How can I help?"}]},
    {"role": "user", "content": [{"text": "What did I just say?"}]},
])

Return value — AgentResult

Every agent call returns an AgentResult:

python
result = agent("Hello")

print(result.message)        # Final assistant message
print(result.stop_reason)    # "end_turn" | "max_tokens" | "tool_use" | "cancelled"
print(result.metrics)        # Token usage, latency
print(result.state)          # Agent state snapshot

Async usage

python
import asyncio
from elsai import Agent

agent = Agent()

async def main():
    result = await agent.invoke_async("Tell me a joke")
    print(result)

asyncio.run(main())

Cancellation

Cancel a running agent from another thread:

python
import threading
from elsai import Agent

agent = Agent()

def cancel_after(seconds):
    import time
    time.sleep(seconds)
    agent.cancel()

threading.Thread(target=cancel_after, args=(5,)).start()
result = agent("Do a very long task...")
print(result.stop_reason)  # "cancelled"

Agent identity

Give agents meaningful names and descriptions — required when using them as tools inside other agents:

python
from elsai import Agent

researcher = Agent(
    name="researcher",
    description="Searches the web and summarises findings on any topic.",
    agent_id="researcher-001",
)

Copyright © 2026 Elsai Foundry.