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 2026")
print(result)

Agent configuration with AgentConfig

Pass agent metadata and execution options through AgentConfig, not as direct Agent(...) keyword arguments:

python
from elsai import Agent
from elsai.agent import AgentConfig

agent = Agent(
    model="us.amazon.nova-pro-v1:0",
    system_prompt="You are a helpful assistant.",
    tools=[my_tool],
    config=AgentConfig(
        agent_id="assistant",
        name="My Agent",
        plugins=[my_plugin],
        hooks=[my_hook],
        session_manager=session,
    ),
)

See the full AgentConfig reference.

Deprecated kwargs

Direct keyword arguments such as Agent(plugins=[...]), Agent(hooks=[...]), or Agent(agent_id=..., session_manager=...) still work but emit a deprecation warning. Use AgentConfig in new code.

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.
load_tools_from_directoryboolFalseAuto-load tools from ./tools/ with hot-reload.
configAgentConfig | NoneNoneAgent metadata and execution options — see table below.

AgentConfig fields

FieldDescription
agent_idIdentifier for sessions and multi-agent (default "default").
nameHuman-readable agent name.
descriptionWhat this agent does (used when wrapping as a tool).
stateInitial shared mutable state accessible to tools.
pluginsPlugin instances to extend functionality.
hooksLifecycle event hooks.
session_managerPersists conversation state across runs.
retry_strategyRetry behaviour on transient errors.
tool_executorControls how parallel tool calls are executed.
limitsDefault per-invocation budget caps (turns, output_tokens, total_tokens) — see Invocation Limits.
sandboxShared Sandbox workspace for isolated execution — see Sandbox API.

For isolated shell and file execution, see Sandbox instead of host prebuilt tools alone.

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" | "limit_turns" | "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
from elsai.agent import AgentConfig

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

Copyright © 2026 elsai foundry.