Skip to main content

Overview

The Agent class is the core component of the Swarms framework, connecting LLMs with tools, memory, and autonomous capabilities. It provides a production-ready interface for building agents that can reason, call tools, handle multimodal inputs, and execute complex tasks.

Import

Key Features

  • Tool Integration: Native support for function calling and tool execution
  • Persistent Memory: Optional MEMORY.md across restarts, plus automatic context compression
  • Autonomous Loops: Dynamic execution with configurable stopping conditions
  • Multi-modal Support: Process text, images, and other media
  • MCP Support: Integration with Model Context Protocol servers
  • Agent Handoffs: Delegate tasks to specialized agents
  • Streaming: Real-time token streaming with callbacks
  • Fallback Models: Automatic failover to backup models
  • State Management: Autosave and state persistence
  • Token Usage: Provider-reported token totals on agent.usage
  • Telemetry: Optional OpenTelemetry tracing of every run

Architecture

Agent delegates its larger subsystems to dedicated collaborator objects, each built during __init__ and reachable as an attribute. Model, fallback, and skills helpers on the agent are thin wrappers over their manager, so both call styles work:
Tool handling has no agent-level wrappers. Apart from add_tool, add_tools, remove_tool, remove_tools, and the mcp_enabled property, you reach it through agent.tool_manager (for example agent.tool_manager.execute_tools(...)). See Tool handling. Each collaborator is independently documented. Reach for it directly when you want the behavior without going through the agent.

Initialization

Agent.__init__ also accepts *args and **kwargs and ignores them. A misspelled keyword argument is silently dropped rather than rejected, so check names against this list.
Optional[str]
default:"None"
Unique identifier for the agent instance. When omitted the agent generates one via generate_id("agent"), of the form agent- followed by 32 hex characters (secrets.token_hex(16)) — not a UUID.
str
default:"swarm-worker-01"
The name of the agent, used for identification and logging. Omit it and the agent defaults to the literal "swarm-worker-01" — every agent that does not set a name shares this same value.
Because the default is a shared literal rather than a generated value, anything keyed on agent_name collides across unnamed agents: concurrent result dictionaries keep one entry for the whole group, and MEMORY.md folders are shared. Pass an explicit name whenever agents run concurrently or need memory to be stable across restarts.
str
A description of the agent’s purpose and capabilities. Shown to orchestrators when routing tasks.
Optional[str]
default:"None"
The system prompt that defines the agent’s behavior and personality. When None, the agent uses the framework’s general-purpose AGENT_SYSTEM_PROMPT_3, rendered with the current time by build_agent_system_prompt(), rather than an empty prompt. Some options append to it during construction: max_loops="auto" (autonomous-mode instructions), reasoning_prompt_on, react_on, handoffs, and dynamic_tools.
Optional[Any]
default:"None"
The language model instance to use. If None, a LiteLLM client is built from the agent’s settings. A caller-supplied LiteLLM client with an unset usage_hook is wired into agent.usage too.
str
default:"gpt-5.4"
The LiteLLM-compatible model identifier (e.g. "gpt-5.4", "claude-sonnet-4-6", "groq/llama-3.3-70b-versatile"). Ignored when fallback_models is set, since that list names the primary model.
dict
default:"None"
Extra keyword arguments forwarded to the underlying LiteLLM client.
bool
default:"False"
Enable provider-side prompt caching. When True, ephemeral cache_control breakpoints are added to the stable prefix of each request (system prompt, tools, and the last message) so it is cached and re-billed at a large discount. Applies to the Anthropic model family (Claude on Anthropic / Bedrock / Vertex); providers that cache automatically (e.g. OpenAI) are left untouched. See the Prompt Caching guide.
dict
default:"None"
Fine-grained prompt-caching options; only consulted when prompt_caching=True. All keys optional:
  • ttl (str): "5m" (default) or "1h" for Anthropic’s extended cache (2x write cost, survives longer gaps; the required beta header is attached automatically).
  • cache_system_prompt (bool, default True): cache the system prefix.
  • cache_messages (bool, default True): cache through the last message (incremental multi-turn caching).
  • cache_tools (bool, default True): cache the tool-definitions block.
  • override (bool, default None): force cache_control injection on/off regardless of the detected provider (e.g. opt Gemini/Vertex in, or a custom alias out). None auto-detects (Anthropic only).
  • prompt_cache_key (str): OpenAI-only routing hint for higher cache hit rates.
  • prompt_cache_retention (str): OpenAI-only cache TTL — "in_memory" or "24h".
str
default:"None"
Base URL for OpenAI-compatible providers (Ollama, LM Studio, vLLM, etc.).
str
default:"None"
Override API key for the LLM provider. Falls back to environment variables when unset.
str
default:"None"
Single fallback model used when the primary model fails. Only consulted when fallback_models is not set.
Optional[Union[int, str]]
default:"1"
Number of loop iterations per run. Pass "auto" for autonomous mode: plan, execute subtasks, then summarize. Anything other than a positive integer or "auto" raises AgentInitializationError.
List[Callable]
default:"None"
Callable functions the agent can use as tools. Their schemas and the tool executor are built during construction, so pass every tool here rather than adding them later.
Optional[float]
default:"None"
Sampling temperature. Left unset, it is omitted from requests and the provider’s default applies. Some models (for example recent Claude models) reject an explicit temperature with HTTP 400, which is why the default is None.
Optional[int]
default:"None"
Maximum number of tokens in the LLM response. A positive value you pass is kept and forwarded to the LLM. Leave it as None (or pass 0/a negative number) and the agent resolves the model’s own maximum output-token limit, falling back to 16000 when that lookup fails.
int
default:"None"
Effective context window in tokens. When context_compression=True, the agent compresses memory once usage crosses 90% of this limit. Leave as None to derive the window from the model’s input limit, falling back to 16000 when that lookup fails.
float
default:"None"
Nucleus-sampling parameter. Left unset, it is not sent. Stripped automatically for Anthropic models when extended thinking is enabled.
bool
default:"True"
When True, the text rendering of the conversation history (short_memory.return_history_as_string()) drops its oldest characters until it fits within context_length tokens. It affects every path that sends or measures the history as text, such as transforms, plan(), and input_tokens.
bool
default:"True"
When True, the agent runs a ContextCompressor that summarises the conversation history once it crosses 90% of context_length, so long sessions never hit the context wall. It checks at the start of every loop iteration, and between subtask iterations in autonomous mode.
bool
default:"False"
When True, read/write MEMORY.md at {WORKSPACE_DIR}/agents/{agent_name}/MEMORY.md so agent state survives process restarts. Off by default: opt in explicitly.
The preload is a System message, and run skips System messages when it builds the request, so the model may never see it. Do not assume the agent “remembers” earlier runs. See Agent memory.
Optional[List[Dict[str, Any]]]
default:"None"
Prior conversation turns in chat format, e.g. [{"role": "user", "content": "..."}]. They seed short_memory at construction and are re-sent as context on every run. Keys other than role and content (name, tool_calls, tool_call_id) are kept as message metadata. To supply turns for a single call instead, pass messages to run.
Union[TransformConfig, dict]
default:"None"
Message transforms (TransformConfig from swarms.structs.transforms, or an equivalent dict) applied to the history before each LLM call. When set, the agent sends a flattened text rendering of the transformed history instead of its structured message transcript.
bool
default:"False"
Stream tokens as they are generated. By default tokens render in a Rich streaming panel. With a streaming_callback, each token string goes to the callback instead. With print_on=False and no callback, the stream is collected silently.
bool
default:"False"
Detailed token-by-token streaming. Every token is printed with its metadata (ResponseStream {...}), and the per-token metadata dict (token, model, finish reason, citations, usage, and so on) is passed to streaming_callback. Takes precedence over streaming_on.
Optional[Callable[[str], None]]
default:"None"
Receives tokens as they stream. It is only called when the agent streams: with streaming_on=True it gets each token string, and with stream=True it gets each per-token metadata dict. With neither flag set it is never called. run_stream and arun_stream turn streaming_on on for you.
bool
default:"False"
REPL-style mode. After each loop iteration the agent prompts you for input, and your reply is sent to the model as the next user turn. Each reply uses one loop, so set max_loops above 1 to hold a conversation. Typing custom_exit_command, or pressing Ctrl+C or Ctrl+D, ends the session. When run() gets no task, an interactive agent prompts for one instead of raising ValueError. Setting it turns reasoning_prompt_on off. It has no effect with max_loops="auto", which always runs the autonomous loop.
bool
default:"False"
Enable verbose logging for debugging.
bool
default:"True"
When False, suppress the agent’s printed output (Rich panels, thinking panel, etc.). Token streams via arun_stream / streaming_callback are unaffected.
OutputType
default:"str-all-except-first"
How the run’s result is formatted. See Output Types for all 15 accepted values.
bool
default:"False"
Save agent state during execution: save() at the start and end of each run and on errors, plus a config.json snapshot in the agent workspace at each loop step. See Autosave.
bool
default:"False"
Display agent dashboard on initialization
Optional[Union[Callable, Any]]
default:"None"
A store that is written out with agent state. The agent never queries it — only .save(path) is ever called on it, so this does not provide RAG. For retrieval, expose the lookup as a tool instead.
List[str]
default:"None"
Ordered list of models to use. When set, it replaces model_name: the first entry is the primary model and the rest are tried in order if a run fails. See Fallback model helpers.
Optional[int]
default:"3"
Attempts per loop iteration for the LLM call. When every attempt fails, the run raises AgentLLMError, and run() then moves on to the next fallback model if one is configured. See Error Handling.
str
default:"None"
Stored on the agent, but the run loop never checks it. To end a run early, use stopping_condition or stopping_func.
Callable[[str], bool]
default:"None"
Called with the response after each loop iteration (integer max_loops). Returning True ends the run.
Callable
default:"None"
Alternative stopping function, checked after stopping_condition in the same way.
bool
default:"False"
At the start of each loop iteration, set the LLM client’s temperature to a random value between 0.0 and 1.0.
bool
default:"False"
Sets max_loops to "auto" during construction, so run() enters the autonomous loop. Unlike passing max_loops="auto" directly, the autonomous-mode instructions are not appended to the system prompt.
int
default:"0"
Seconds to wait between consecutive loop iterations.
str
default:"exit"
Text the user can type in interactive mode to exit the loop. Compared case-insensitively.
bool
default:"False"
Sets agent.stopping_token to "<DONE>". The check is is not None, so this also happens with the default False. Nothing in the run loop reads stopping_token.
bool
default:"False"
At the start of each run (integer max_loops), set agent.system_prompt to a prompt the LLM generates from agent_name, agent_description, and the current system_prompt, and record it in the conversation. The task is used only when all three are empty. The LLM client keeps the system prompt it was built with until you call agent.llm_handling().
str
default:"Human"
Name of the user in conversation history
str
default:"Auto-generated"
Path that save() writes to when called with no argument. When unset, falls back to an auto-generated {api_key}_state.json filename inside the agent workspace.
str
default:"None"
Standard operating procedure. Added to the conversation as a user message during construction.
List[str]
default:"None"
List of standard operating procedures, joined with newlines into sop and added to the conversation during construction.
str
default:"None"
Rules that govern agent behavior, added to the conversation after the system prompt.
str
default:"None"
Replaces the default planning instruction that plan() sends when plan_enabled=True.
bool
default:"False"
Run plan(task) at the start of each run (integer max_loops). The plan is added to the conversation before the first loop.
Optional[bool]
default:"None"
When True, sets sop to the framework’s multi-modal instruction prompt, which is added to the conversation during construction. Images are accepted through run(img=..., imgs=...) whether or not this is set.
bool
default:"True"
After callable tools run, make a separate LLM call that summarises their output and add the summary to the conversation. MCP tool results are always summarised this way.
int
default:"3"
Total attempts for a batch of callable tool calls. Each attempt runs the batch once. If every attempt fails, AgentToolExecutionError is raised inside the loop, the failure is recorded in the conversation, and the run continues so the model can react to it. Also used as the MCP retry count.
bool
default:"True"
Display tool inputs/outputs in the agent’s printed output.
Optional[List[Dict[str, Any]]]
default:"None"
OpenAI function-calling schemas sent to the model as-is. Calls to a schema with no matching callable in tools are not executed: the tool call itself (name plus arguments) becomes the response, which is how you get structured output.
Union[BaseModel, Dict[str, Any]]
default:"None"
A Pydantic model (or dict) converted to a function-calling schema and added to the conversation as a message during construction. It is not registered as a callable tool.
Callable
default:"None"
Stored on the agent, but the run loop does not apply it. agent.output_cleaner_op(response) runs it and records the cleaned text in the conversation under the role "Output Cleaner".
List[BaseModel]
default:"None"
Pydantic models meant to be added to the conversation as function-calling schemas.
Passing a non-empty list_base_models currently raises TypeError during construction. ToolManager.handle_tool_schema_ops calls BaseTool.multi_base_models_to_dict without the list. Convert each model with BaseTool().base_model_to_dict(Model) (from swarms.tools.base_tool) and pass the results in tools_list_dictionary instead.
Optional[Union[str, MCPConnection, Dict]]
default:"None"
A single MCP server. Pass a URL string for an unauthenticated server, or an MCPConnection/dict to configure auth, transport, headers and timeouts.
Optional[List[Union[str, MCPConnection, Dict]]]
default:"None"
Several MCP servers. Each entry may be a URL string, an MCPConnection, or a dict. Tools from every server are merged and each tool call is routed back to the server that owns it.
Optional[Union[MCPConnection, Dict]]
default:"None"
A single MCP server given as a connection object (or the equivalent dict).
Optional[List[Union[MCPConnection, Dict]]]
default:"None"
Several MCP servers given as connection objects (or dicts).
Optional[str]
default:"None"
API key applied to every MCP server that does not define its own. Sent as Authorization: Bearer <key> by default; override the header or prefix per-server with MCPConnection(api_key_header=..., api_key_prefix=...). Supports "env:MY_VAR" / "${MY_VAR}" indirection so secrets stay out of code.
Optional[str]
default:"None"
Bearer token applied to every MCP server that does not define its own. Equivalent to mcp_api_key with the default header and prefix.
Optional[Union[MCPOAuthConfig, Dict]]
default:"None"
OAuth 2.1 settings applied to every MCP server without its own. Supports the interactive authorization-code flow (PKCE plus dynamic client registration, tokens cached on disk), the headless client_credentials grant, and pre-issued access tokens.
Optional[Dict[str, str]]
default:"None"
Extra headers merged into every MCP request.
Optional[Literal['streamable_http', 'sse', 'stdio', 'auto']]
default:"None"
Force a transport for every MCP server, overriding each connection’s own setting. With None, each connection uses its own transport, which defaults to streamable_http, so a URL is not auto-detected. Pass "auto" to detect from the URL: HTTP URLs become streamable_http (or sse when the URL looks like an SSE endpoint), and a value with no scheme runs as a stdio command.
Optional[int]
default:"None"
HTTP request timeout in seconds for every MCP server. Falls back to the per-connection default of 30. It does not bound how long a tool call may run. Set tool_timeout (default 120 seconds) on the MCPConnection for that.
Union[Sequence[Callable], Any]
default:"None"
Agents this agent can delegate to, as a list or a {name: agent} dict. The agent gets a handoff_task tool and a prompt listing each target’s name and description. See Agent Handoffs and Task Delegation.
List[str]
default:"None"
Free-form capability labels. Sent along with tags when the agent is published to the marketplace. The agent itself does not read them.
agent_roles
default:"worker"
The agent’s role within a swarm (e.g. "worker", "director"). One of the agent_roles literal values in swarms.structs.agent_roles.
List[str]
default:"None"
Tags used to categorise the agent. Sent to the marketplace on publish.
List[Dict[str, Any]]
default:"None"
Intended use cases, each a dict with title and description. Required when publishing to the marketplace.
Literal['interactive', 'fast', 'standard']
default:"standard"
Only "fast" changes behavior: it sets print_on=False and verbose=False at the end of construction. "interactive" and "standard" change nothing. For a REPL, set interactive=True.
str
default:"None"
UUID of a Swarms marketplace prompt. It is fetched during construction and appended to the system prompt, and it fills in agent_name / agent_description when they are still at their defaults. Requires SWARMS_API_KEY.
bool
default:"False"
When True, publish this agent’s prompt and metadata to the Swarms marketplace during construction. Requires use_cases (otherwise AgentInitializationError) and SWARMS_API_KEY.
Optional[Union[str, List[str]]]
default:"None"
Directory of Agent Skills (Anthropic SKILL.md format), or a list of directories. Directories are read in order, and when two hold a skill with the same name the later one wins and a warning is logged. The constructor annotation says str, but a list is passed straight through to SkillsManager.On each run the agent selects the skills whose descriptions match the task and sends them with every LLM call: as a system message when the call carries a message list, otherwise in front of the task text. system_prompt is not modified. See Skills helpers.
Optional[Union[str, List[str]]]
default:"all"
Tools to enable for the autonomous looper when max_loops="auto". Use "all", or a list of tool names — the list filters the entire loop tool set, control-flow tools included, so include create_plan and complete_task unless you intend to remove them. get_autonomous_loop_tool_names() from swarms.structs.autonomous_loop_utils returns every valid name.
bool
default:"False"
Give the autonomous looper (max_loops="auto") a think tool for an explicit reasoning turn. Off by default: it costs a full round-trip to produce reasoning most models can emit inline alongside their actions. When False the system prompt is adjusted to match, so the model is never told to call a tool it does not have.
int
default:"5"
Autonomous mode only. How many times to ask the model for a plan before the run fails.
int
default:"100"
Autonomous mode only. Ceiling on passes of the execution phase across the whole run. Each pass picks the next runnable subtask and works it for up to max_subtask_loops iterations. When the ceiling is reached, execution stops and the agent moves to the final summary.
int
default:"20"
Autonomous mode only. Ceiling on iterations spent inside any one subtask. A subtask that exhausts it is marked failed and the loop moves on.
All three budgets must be at least 1. A smaller value raises ValueError during construction.
bool
default:"False"
Defer tool schemas behind a tool_search tool instead of sending them all on every request. Off by default; opt in with True. See Dynamic Tool Loading below.
bool
default:"False"
Append the framework’s ReAct prompt to the system prompt.
bool
default:"True"
When max_loops is 2 or more (or "auto"), append the framework’s reasoning preamble to the system prompt and, for integer max_loops, add a Current Internal Reasoning Loop: i/N message at each iteration. interactive=True turns it off.
bool
default:"False"
Enable reasoning mode on the LLM client. Combined with thinking_tokens it sends an extended-thinking request. On models LiteLLM lists as reasoning-capable, the temperature is set to 1.
Optional[Literal['none', 'minimal', 'low', 'medium', 'high', 'xhigh', 'ultra', 'max', 'None']]
default:"None"
Effort level for reasoning models: "minimal", "low", "medium", "high", "xhigh", "ultra", "max", or "none". The annotation is the ReasoningEffort literal in swarms.utils.get_reasoning_efforts.Left unset, the parameter is not sent to the provider at all.
Do not combine reasoning_effort with tools on OpenAI reasoning models such as gpt-5.4-mini. /v1/chat/completions rejects the pair with BadRequestError: Function tools with reasoning_effort are not supported. This is why the default is None: it previously defaulted to "medium", which shipped on every request and made Agent(model_name="gpt-5.4-mini", tools=[...]) fail out of the box.
int
default:"1024"
Extended-thinking budget. Sent as thinking={"type": "enabled", "budget_tokens": thinking_tokens} when reasoning_enabled=True. For Anthropic models, max_tokens is raised above the budget when needed.
bool
default:"False"
Append the framework’s safety prompt to the system message stored in the agent’s conversation.
bool
default:"False"
Once, during construction, set model_name to a random pick from the built-in model list in swarms.structs.ma_utils. The LLM client is built just before this, so calls keep using the original model until the client is rebuilt.
str
Not a constructor argument — any value passed here is ignored. The workspace root is read from the WORKSPACE_DIR environment variable; set it explicitly (e.g. via a .env file — .env.example uses agent_workspace). When it is unset, agent.workspace_dir resolves to None and an error is logged; the workspace manager behind agent.workspace then falls back to {cwd}/agent_workspace the first time it needs a directory. Each agent gets its own subdirectory at {workspace}/agents/{agent-name}-{id12}/, where id12 is the last 12 characters of agent.id. Read the resolved path from agent.workspace.dir.
str
default:"None"
Default path for load() when it is called without an argument. Nothing is loaded during construction.

Methods

run

Execute the agent’s main loop for a given task. With max_loops="auto" it runs the autonomous loop; otherwise it runs up to max_loops iterations.
Union[str, Any]
The task or prompt for the agent to process. Non-string values are formatted to text. An empty task raises ValueError unless interactive=True, in which case the agent prompts for one.
str
Optional image for vision-enabled models: a file path, URL, data URI, or raw base64 string.
List[str]
Optional list of images, sent together in one request. Ignored when max_loops="auto".
In this release img and imgs reach the model only when the agent was built with transforms. On the default path the agent sends its structured message transcript, and the LiteLLM wrapper does not attach images to a prebuilt message list, so the image is silently dropped. The autonomous loop has the same limitation. Until this is fixed, build vision agents with transforms=TransformConfig() (from swarms.structs.transforms), which sends the history as text plus the image.
str
Accepted for compatibility but not used to check the output. It is only forwarded to fallback-model runs.
Callable[[str], None]
Overrides the agent’s streaming_callback for this call. It is only called when the agent streams (streaming_on=True or stream=True).
int
default:"1"
Number of samples. When n > 1, the task runs n times, each sample receives every input (img, imgs, streaming_callback, messages), and run returns a list of n results. The samples share the agent’s conversation. Ignored when max_loops="auto".
List[Dict[str, Any]]
Prior conversation as typed chat turns. When given, these replace the transcript the agent would otherwise derive from its own memory, and task is appended as the new user turn. The turns are also recorded in short_memory, and with max_loops="auto" the autonomous loop’s transcript starts from them. Multi-agent structures use this to hand an agent the shared room with roles intact — the agent’s own turns as assistant, everyone else’s as labelled user turns — instead of one flattened string.
Any
Agent output formatted according to output_type configuration
Return types based on input: Raises: ValueError for an empty task outside interactive mode. AgentLLMError, chained from the last provider error, once every retry and every fallback model has failed. See Error Handling. Examples:
To receive tokens as they arrive, build the agent with streaming_on=True and pass a callback:

call

Alternative syntax for running the agent (calls run internally).

arun

Async version of run. It runs run in a worker thread with asyncio.to_thread, passing positional arguments in run’s order.

run_batched

Run multiple tasks sequentially, one after another, and collect the results. For concurrent execution use run_concurrent_tasks instead.
List[str]
List of tasks to run, in order
List[str]
default:"None"
One image per task, paired by position. Omit to run the tasks without images. A length mismatch raises ValueError rather than silently dropping the extras.
List[Any]
List of results from each task execution, in the same order as the input tasks

run_stream

Run the agent and yield response tokens one-by-one as a sync generator. It calls run in a background daemon thread with streaming_on switched on for the duration, so every max_loops value takes its normal path, and max_loops="auto" enters the autonomous loop. Tokens are forwarded to the caller the moment the LLM emits them. Extra kwargs go to run, and an exception raised by the run is re-raised after the stream ends.
Tool-call results are fed back into the loop automatically — tokens from each subsequent LLM turn (synthesis turn, autonomous summary phase, etc.) are streamed through as well.

arun_stream

Async generator version of run_stream. run executes in a background thread while tokens are forwarded through an asyncio.Queue, so the caller’s event loop is never blocked.
Both run_stream and arun_stream work for any max_loops value (1, integer > 1 with tools, or "auto"). They stream tokens through every internal loop, including tool-call turns, synthesis turns after a tool returns, and the autonomous plan/execute/summary cycle.

run_concurrent_tasks

Run a batch of tasks concurrently on a call-scoped thread pool (os.cpu_count() workers). Results come back in the order the tasks were given. A failure is logged and re-raised.
Every task runs on the same agent and shares its short_memory. For isolated runs, give each task its own agent.

bulk_run

Generate responses for multiple input sets, one after another. Each input is a dict of kwargs forwarded to run. If any run raises, the error is logged and bulk_run returns None.

save

Save the agent’s current state to a JSON file via SafeStateManager, writing atomically through a temporary file. A relative file_path is resolved inside the agent workspace (agent.workspace.dir); an absolute path is used as-is. .json is appended when missing. With no argument, saved_state_path is used. long_term_memory, when set, is saved alongside as {name}_memory.json.

load

Load agent state from a saved JSON file via SafeStateManager. If file_path is omitted, falls back to load_state_path, then f"{saved_state_path}.json". Unlike save, a relative path is resolved against the current working directory, not the agent workspace, so pass an absolute path to load what save wrote.

save_to_yaml

Save the agent to a YAML file.

to_dict

Convert every agent attribute (except llm) to a dictionary. Callables become {"name", "doc"} dicts, and values that cannot be serialized become "<Non-serializable: TypeName>".

to_json

Convert agent configuration to JSON string. Extra arguments go to json.dumps.

to_yaml

Convert agent configuration to YAML string. Extra arguments go to yaml.dump.

to_toml

Convert agent configuration to TOML string. Extra arguments go to toml.dumps.

model_dump_json / model_dump_yaml

Save the agent configuration to {agent_name}.json or {agent_name}.yaml in the agent workspace, and return a message naming the path.

add_tool / add_tools / remove_tool / remove_tools

Append callables to agent.tools, or remove them from it.
These only edit the agent.tools list. The schemas sent to the model and the executor that runs tool calls (agent.tool_struct) are built during construction and are not rebuilt, so the model cannot call a tool added afterwards. agent.tools is None on an agent built without tools, so add_tool raises AttributeError there. Pass every tool to the constructor instead.

add_memory

Append a message to the agent’s short-term memory under the agent’s own name.

talk_to

Run task on this agent, then send the output to agent as "From {agent_name}: Message: {output}" and return that agent’s response.

talk_to_multiple_agents

Run talk_to against several agents concurrently on a call-scoped thread pool.
Returns one entry per agent, in the order the agents were given. An agent whose conversation raised contributes None.

receive_message / send_agent_message

receive_message wraps an incoming message from another agent in a short preamble and runs it through the agent’s normal run() loop, returning the agent’s response. send_agent_message prefixes a message with To: {agent_name}: and runs that through run(), returning the result.
Task delegation via handoffs happens automatically inside run() — the LLM calls an internal handoff tool when it decides to delegate. There is no separate public handle_handoffs() method to call directly.

reset

Drop the agent’s short-term memory by setting agent.short_memory = None. Nothing is re-initialized in its place.
After reset() the agent cannot run again until short_memory is replaced with a fresh Conversation. Construct a new agent instead unless you intend to rebuild it yourself.

plan

Ask the LLM for a step-by-step plan for task (using planning_prompt when set) and add the plan to the conversation. Nothing is executed and nothing is returned.
Display the agent’s configuration dashboard.

showcase_config

Display the agent’s configuration in a formatted table.

update_system_prompt / update_max_loops / update_loop_interval

In-place setters for runtime reconfiguration. Each one only sets the attribute.
The LLM client copies the system prompt when it is built, so after update_system_prompt call agent.llm_handling() to rebuild the client before the next run. Switching to max_loops="auto" with update_max_loops does not append the autonomous-mode instructions that the constructor adds.

Tool handling

Tool setup, parsing, execution, retries, MCP execution, handoffs, and dynamic tool loading live on agent.tool_manager, a ToolManager (from swarms.agents.tool_manager) built once per agent. The agent no longer has wrapper methods for them. get_all_selected_tools() was removed. Use get_autonomous_loop_tool_names() from swarms.structs.autonomous_loop_utils to list every autonomous-loop tool name:

Properties and attributes

Notable attributes, all set during construction:

get_llm_parameters

Returns the parameters of the language model as a string (str(vars(self.llm))).

Fallback model helpers

Methods backing the fallback_models / fallback_model_name feature. All delegate to LLMManager.

Skills helpers

Agent Skills loading, delegating to SkillsManager.
agent.skills_dir and agent.skills_metadata are properties that read and write through to the manager.

Marketplace helpers

Marketplace integration, delegating to AgentMarketplaceHandler.
Setting marketplace_prompt_id loads a prompt during construction; setting publish_to_marketplace=True publishes during construction.

Complete Methods Reference

Every public method on Agent. Tool-handling methods now live on agent.tool_manager; see Tool handling.

Advanced Capabilities

Tool Integration

The Agent class allows seamless integration of external tools by accepting a list of Python functions via the tools parameter. Each tool function must include type annotations and a docstring. The agent automatically converts these functions into an OpenAI-compatible function calling schema.
You can also provide tool schemas in OpenAI function-calling dictionary format via tools_list_dictionary:

Dynamic Tool Loading

dynamic_tools is off by default (False): every tool schema is sent on every request. Opt in with dynamic_tools=True to change how tools reach the model. Tool definitions are re-sent on every request, so a large tool set is paid for on every turn. When dynamic_tools=True and the agent has something to defer — that is, when tools are given, an MCP server is configured, or max_loops="auto" — the agent:
  1. Appends a notice to the system prompt telling the model that most of its tools are not currently loaded.
  2. Sends only a tool_search tool (plus anything marked always-loaded, such as the autonomous loop’s control-flow tools create_plan, think (when think_tool=True), subtask_done, complete_task, and respond_to_user) instead of the full schema list.
  3. Loads the schemas the model asks for through tool_search. They become callable on the next turn, not the one that searched.
An agent with no tools, no MCP server, and a fixed max_loops has nothing to defer, so nothing changes.
With dynamic_tools=True and max_loops="auto", the loop’s own file and bash tools are deferred too. A model that searches for a capability the catalog does not hold gets the closest lexical match rather than a miss, which can stall a subtask. This is why the default changed to False.
Deferral is a separate mechanism from selected_tools. selected_tools decides which autonomous-loop tools exist at all; dynamic_tools decides which of the existing schemas are sent up front.
See the Dynamic Tools guide and the DynamicToolLoader reference for the search behaviour, pre-warming, and the always-loaded set.

External Knowledge and Retrieval

Swarms bundles no vector database, and Agent performs no retrieval of its own. To give an agent access to an external knowledge base, expose the lookup as a tool — the agent then decides when to query it and the result enters the conversation like any other tool output.
long_term_memory is a constructor parameter, but the agent never queries it. The only method invoked on it is .save(path), and only when agent state is written to disk. Passing a vector store here does not give the agent retrieval — use a tool, as above.

Memory Persistence and Context Compression

The agent ships with two complementary memory controls that work together to manage what is remembered between runs and how the context window is managed during long sessions.

Persistent Memory

With persistent_memory=True the agent reads and writes a MEMORY.md file under $WORKSPACE_DIR/agents/{agent_name}/MEMORY.md. Every message is appended to this file, and the next agent constructed with the same name loads it back into agent.short_memory as a System message. The default is persistent_memory=False — no on-disk state at all, so the agent starts from a blank slate every run. If you opt in, use the same agent_name each time, since the memory file is keyed on it.
Loading the file does not guarantee the model reads it. run builds its request from the non-System turns (or, with max_loops="auto", from the task and messages), and the preload is a System message. To give the model prior context, pass it in system_prompt, the task, or messages. See Agent memory.

Context Compression

For long-running agents the conversation history can grow until it fills the context window. context_compression=True (the default) attaches a ContextCompressor that summarises the conversation history with the agent’s own model whenever token usage crosses 90% of context_length, and replaces the history with that summary. The system prompt and rules are kept. With persistent_memory=True, MEMORY.md is wiped and re-seeded at the same time, so it stops growing. The check runs at the start of every loop iteration. In autonomous mode it also runs between subtask iterations, and the loop rebuilds the request it sends around the summary, so the payload shrinks too. The summary call is not counted in agent.usage.

Combining Both Controls

Token usage

Two read-only properties report token counts. See the Token usage guide for walkthroughs.
The total lives on the agent rather than the client, so it survives the client being rebuilt by tool_search or a fallback-model switch.

Agent Handoffs and Task Delegation

The Agent class supports task delegation through the handoffs parameter. Give it a list of specialized agents (or a {name: agent} dict) and the model can delegate work to them during a normal run. How Handoffs Work:
  1. Setup: During construction the agent registers a handoff_task tool and appends a prompt to its system prompt that lists each target’s agent_name and agent_description.
  2. Delegation: When the model decides to delegate, it calls handoff_task with one or more requests, each holding agent_name, task, and reasoning.
  3. Execution: agent.tool_manager runs each named agent on its task. Several requests run in parallel.
  4. Results: The combined responses are recorded in the conversation as a Handoff Result, where the next loop iteration (or the output) can use them.

Autonomous Mode

When max_loops="auto" is set, run() hands the task to the autonomous loop (agent.autonomous_loop). The agent creates a structured plan with subtasks, executes them in dependency order, and generates a comprehensive summary. run_stream and arun_stream take the same path, and interactive has no effect here. See the Autonomous mode guide for a walkthrough. Three constructor parameters bound the cost of a run: When every subtask is done, the agent asks the model for a final summary through complete_task. If the model does not call it, the agent writes a summary itself from the subtask results. Either way the result is shaped by output_type, so output_type="final" returns that summary message rather than a different type depending on the path. When context_compression is on, the ContextCompressor also runs between subtask iterations. After a compression, the request sent to the model is rebuilt from the summary plus the current subtask prompt.

Available Tools in Autonomous Mode

When max_loops="auto", the agent has access to specialized tools: Your own tools, MCP tools, and the handoff_task tool (when handoffs is set) are available alongside these. Output caps. read_file, run_bash, grep, and glob cap their output at a quarter of context_length tokens (4,096 tokens when there is no context length), counted with the model’s tokenizer. A truncated result ends with a note giving how many tokens were kept out of the total, so the model can narrow its query. Bash guardrails. run_bash refuses commands that match a blocklist of dangerous patterns (recursive deletes, piping into a shell, sudo, raw disk writes, and similar). Commands over 512 characters are refused too. When an over-length command carries a heredoc or redirect, the refusal tells the model to write the file with create_file or update_file instead.
selected_tools filters this entire list, including the control-flow tools. A list that omits create_plan leaves the model unable to plan, and one that omits subtask_done leaves it unable to close a subtask, so each subtask ends only when it runs out of max_subtask_loops. Omitting complete_task is safe: the agent writes the final summary itself. Include the tools you need, or leave selected_tools="all".
think is opt-in. It is stripped from the tool list unless the agent is constructed with think_tool=True, and the system prompt is adjusted to match so the model is never told to call a tool it lacks.
The file tools, grep, and glob resolve relative paths against the agent’s workspace directory ($WORKSPACE_DIR/agents/{agent-name}-{id12}/, reachable as agent.workspace.dir). run_bash runs in the process’s current working directory, not the workspace.

Sub-Agent Delegation

The autonomous agent can create and manage sub-agents for parallel task execution. Each sub-agent gets the parent’s model and the parent’s tools. A sub-agent with tools runs with max_loops=5 so it can call a tool and read the result; one without tools runs with max_loops=1. Sub-agents never run in autonomous mode, so they cannot recurse.

Batch Processing

Run a list of tasks one after another with run_batched. It is sequential, not concurrent — reach for run_concurrent_tasks when the tasks are independent and you want them in parallel:
Images are subject to the same limitation described under run.

Examples

Basic Usage

Minimal Configuration

Agent with Tools

Multi-modal Agent

Both vision examples set transforms, because images are only attached on that path in this release. See the warning under run.

Multi-Image Processing

Autonomous Agent with Auto Loops

The agent creates a plan with subtasks, executes each one with its tools, then writes a final summary.

Multiple Loops

Dynamic Loops

dynamic_loops=True switches max_loops to "auto" during construction, so the run goes through the autonomous loop.

Agent with Streaming

With streaming_on=True, the callback receives each token as a string.

Token-by-Token Streaming

With stream=True, every token is printed with its metadata, and a streaming_callback would receive the per-token metadata dict instead of a string.

Agent with Fallback Models

The first entry is the primary model; the others are tried in order if a run fails.

Agent with MCP Integration

To run a local MCP server over stdio, pass command and args in mcp_config. A string in mcp_url is never run as a command; it must start with http:// or https://.

Multiple MCP Connections

MCP with Connection Config

Agent Handoffs

Interactive Mode

Each reply you type is sent to the model as the next turn and uses one loop, so max_loops caps the length of the session. Type exit (the default custom_exit_command) to stop early.

Auto Generate Prompt

The generated prompt is stored on agent.system_prompt. Call agent.llm_handling() to send it as the system message on later runs.

Reasoning-Enabled Models

Execution Modes

Only mode="fast" changes anything: it turns print_on and verbose off. For a REPL, use interactive=True instead of mode="interactive".

Marketplace Prompt Loading

Publishing to Marketplace

Message Transforms for Context Management

Agent with Capabilities

Saving and Loading State

save writes a relative path inside the agent workspace, while load reads a relative path from the current working directory, so use an absolute path to round-trip.

Autosave

When autosave=True, the agent saves its configuration at each loop step to {workspace}/agents/{agent-name}-{id12}/config.json via its WorkspaceManager. Files are written atomically to prevent corruption.
The workspace root comes from the WORKSPACE_DIR environment variable. Autosave goes through WorkspaceManager, which falls back to {cwd}/agent_workspace (and sets WORKSPACE_DIR to it) when the variable is unset. Other readers are stricter: agent.workspace_dir, resolved during construction, is None when the variable was unset at that moment. Set WORKSPACE_DIR explicitly rather than relying on the fallback.

Async and Concurrent Execution

Comprehensive Agent Configuration

Various Settings

Output Types

output_type controls what run returns. The accepted values are the HistoryOutputType literal in swarms.utils.output_types (also exported as OutputType).
"basemodel" passes the type check but the formatter has no branch for it, so a run that ends with output_type="basemodel" raises ValueError. XML output was removed, so "xml" is not accepted either.

Error Handling

The agent exception classes live in swarms.schemas.agent_errors and are re-exported from swarms.schemas and the top-level swarms package. All of them extend AgentError.
When the primary model fails, run retries with the next model in fallback_models (see Fallback model helpers). Once the list is exhausted, the original exception is re-raised unchanged. Other failures, such as an authentication error from the provider, also propagate as their own exception types.
The classes are not all importable from the same place. swarms.structs.agent re-exports only AgentInitializationError, AgentLLMError, AgentRunError, and AgentToolExecutionError. from swarms.structs.agent import AgentError raises ImportError. Import from swarms or swarms.schemas instead.
MCP failures use a separate hierarchy in swarms.schemas.agent_mcp_errors: AgentMCPError, AgentMCPConnectionError, and AgentMCPToolError. These extend Exception, not AgentError, and are also re-exported from swarms.schemas.

Telemetry

Telemetry is on by default. Each Agent.run call emits an OpenTelemetry span with the task, the output, the status, and the agent’s identity. Construction emits an Agent.init span with the constructor configuration. Failed model calls and failed tools emit Agent.llm_error and Agent.tool_error spans, and autosave also sends a swarms.state snapshot of the agent. Spans are exported over OTLP/HTTP to a Swarms telemetry endpoint. Set SWARMS_TELEMETRY_ON to false, 0, no, off, disable, or disabled to opt out. An empty value also turns it off. The switch is read once per process, so restart after you change it. See the Telemetry guide for what is captured.

New features and parameters

Every constructor parameter, including the MCP, reasoning, memory, tool, and marketplace options, is documented under Initialization. Per-call options such as imgs, correct_answer, and streaming_callback are under run.

Best Practices

Agent subsystems Everything else
  • Tools - Creating and using agent tools
  • Autonomous mode - Plan, execute, and summarize with max_loops="auto"
  • Token usage - Reading agent.usage
  • Memory - MEMORY.md, compaction, and what the model sees
  • Telemetry - Tracing agent and swarm runs