#
Agents

> Smolagents is an experimental API which is subject to change at any time. Results returned by the agents can vary as the APIs or underlying models are prone to change.

To learn more about agents and tools make sure to read the introductory guide. This page contains the API docs for the underlying classes.

##
Agents

Our agents inherit from MultiStepAgent, which means they can act in multiple steps, each step consisting of one thought, then one tool call and execution. Read more in this conceptual guide.

We provide two types of agents, based on the main `Agent` class.

- CodeAgent writes its tool calls in Python code (this is the default).
- ToolCallingAgent writes its tool calls in JSON.

Both require arguments `model` and list of tools `tools` at initialization.

###
Classes of agents

###
class smolagents.MultiStepAgent

< source >

( tools: list model: Model prompt_templates: smolagents.agents.PromptTemplates | None = None instructions: str | None = None max_steps: int = 20 add_base_tools: bool = False verbosity_level: LogLevel = <LogLevel.INFO: 1> managed_agents: list | None = None step_callbacks: list[collections.abc.Callable] | dict[typing.Type[smolagents.memory.MemoryStep], collections.abc.Callable | list[collections.abc.Callable]] | None = None planning_interval: int | None = None name: str | None = None description: str | None = None provide_run_summary: bool = False final_answer_checks: list[collections.abc.Callable] | None = None return_full_result: bool = False logger: smolagents.monitoring.AgentLogger | None = None )

Parameters

- tools (`list[Tool]`) — Tools that the agent can use.
- model (`Callable[[list[dict[str, str]]], ChatMessage]`) — Model that will generate the agent’s actions.
- prompt_templates (PromptTemplates, optional) — Prompt templates.
- instructions (`str`, optional) — Custom instructions for the agent, will be inserted in the system prompt.
- max_steps (`int`, default `20`) — Maximum number of steps the agent can take to solve the task.
- add_base_tools (`bool`, default `False`) — Whether to add the base tools to the agent’s tools.
- verbosity_level (`LogLevel`, default `LogLevel.INFO`) — Level of verbosity of the agent’s logs.
- managed_agents (`list`, optional) — Managed agents that the agent can call.
- step_callbacks (`list[Callable]` | `dict[Type[MemoryStep], Callable | list[Callable]]`, optional) — Callbacks that will be called at each step.
- planning_interval (`int`, optional) — Interval at which the agent will run a planning step.
- name (`str`, optional) — Necessary for a managed agent only - the name by which this agent can be called.
- description (`str`, optional) — Necessary for a managed agent only - the description of this agent.
- provide_run_summary (`bool`, optional) — Whether to provide a run summary when called as a managed agent.
- final_answer_checks (`list[Callable]`, optional) — List of validation functions to run before accepting a final answer. Each function should:
  - Take the final answer and the agent’s memory as arguments.
  - Return a boolean indicating whether the final answer is valid.
- return_full_result (`bool`, default `False`) — Whether to return the full `RunResult` object or just the final answer output from the agent run.

Agent class that solves the given task step by step, using the ReAct framework: While the objective is not reached, the agent will perform a cycle of action (given by the LLM) and observation (obtained from the environment).

####
extract_action

< source >

( model_output: str split_token: str )

Parameters

- model_output (`str`) — Output of the LLM
- split_token (`str`) — Separator for the action. Should match the example in the system prompt.

Parse action from the LLM output

####
from_dict

< source >

( agent_dict: dict **kwargs ) →
`MultiStepAgent`

Parameters

- agent_dict (`dict[str, Any]`) — Dictionary representation of the agent.
- **kwargs — Additional keyword arguments that will override agent_dict values.

Returns

`MultiStepAgent`

Instance of the agent class.

Create agent from a dictionary representation.

####
from_folder

< source >

( folder: str | pathlib.Path **kwargs )

Parameters

- folder (`str` or `Path`) — The folder where the agent is saved.
- **kwargs — Additional keyword arguments that will be passed to the agent’s init.

Loads an agent from a local folder.

####
from_hub

< source >

( repo_id: str token: str | None = None trust_remote_code: bool = False **kwargs )

Parameters

- repo_id (`str`) — The name of the repo on the Hub where your tool is defined.
- token (`str`, optional) — The token to identify you on hf.co. If unset, will use the token generated when running `huggingface-cli login` (stored in `~/.huggingface`).
- trust_remote_code(`bool`, optional, defaults to False) — This flags marks that you understand the risk of running remote code and that you trust this tool. If not setting this to True, loading the tool from Hub will fail.
- kwargs (additional keyword arguments, optional) — Additional keyword arguments that will be split in two: all arguments relevant to the Hub (such as `cache_dir`, `revision`, `subfolder`) will be used when downloading the files for your agent, and the others will be passed along to its init.

Loads an agent defined on the Hub.

> Loading a tool from the Hub means that you’ll download the tool and execute it locally. ALWAYS inspect the tool you’re downloading before loading it within your runtime, as you would do when installing a package using pip/npm/apt.

####
initialize_system_prompt

< source >

( )

To be implemented in child classes

####
interrupt

< source >

( )

Interrupts the agent execution.

####
provide_final_answer

< source >

( task: str ) →
`str`

Parameters

- task (`str`) — Task to perform.
- images (`list[PIL.Image.Image]`, optional) — Image(s) objects.

Returns

`str`

Final answer to the task.

Provide the final answer to the task, based on the logs of the agent’s interactions.

####
push_to_hub

< source >

( repo_id: str commit_message: str = 'Upload agent' private: bool | None = None token: bool | str | None = None create_pr: bool = False )

Parameters

- repo_id (`str`) — The name of the repository you want to push to. It should contain your organization name when pushing to a given organization.
- commit_message (`str`, optional, defaults to `"Upload agent"`) — Message to commit while pushing.
- private (`bool`, optional, defaults to `None`) — Whether to make the repo private. If `None`, the repo will be public unless the organization’s default is private. This value is ignored if the repo already exists.
- token (`bool` or `str`, optional) — The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated when running `huggingface-cli login` (stored in `~/.huggingface`).
- create_pr (`bool`, optional, defaults to `False`) — Whether to create a PR with the uploaded files or directly commit.

Upload the agent to the Hub.

####
replay

< source >

( detailed: bool = False )

Parameters

- detailed (bool, optional) — If True, also displays the memory at each step. Defaults to False. Careful: will increase log length exponentially. Use only for debugging.

Prints a pretty replay of the agent’s steps.

####
run

< source >

( task: str stream: bool = False reset: bool = True images: list['PIL.Image.Image'] | None = None additional_args: dict | None = None max_steps: int | None = None return_full_result: bool | None = None )

Parameters

- task (`str`) — Task to perform.
- stream (`bool`) — Whether to run in streaming mode. If `True`, returns a generator that yields each step as it is executed. You must iterate over this generator to process the individual steps (e.g., using a for loop or `next()`). If `False`, executes all steps internally and returns only the final answer after completion.
- reset (`bool`) — Whether to reset the conversation or keep it going from previous run.
- images (`list[PIL.Image.Image]`, optional) — Image(s) objects.
- additional_args (`dict`, optional) — Any other variables that you want to pass to the agent run, for instance images or dataframes. Give them clear names!
- max_steps (`int`, optional) — Maximum number of steps the agent can take to solve the task. if not provided, will use the agent’s default value.
- return_full_result (`bool`, optional) — Whether to return the full `RunResult` object or just the final answer output. If `None` (default), the agent’s `self.return_full_result` setting is used.

Run the agent for the given task.

Example:

    from smolagents import CodeAgent
    agent = CodeAgent(tools=[])
    agent.run("What is the result of 2 power 3.7384?")

####
save

< source >

( output_dir: str | pathlib.Path relative_path: str | None = None )

Parameters

- output_dir (`str` or `Path`) — The folder in which you want to save your agent.

Saves the relevant code files for your agent. This will copy the code of your agent in `output_dir` as well as autogenerate:

- a `tools` folder containing the logic for each of the tools under `tools/{tool_name}.py`.
- a `managed_agents` folder containing the logic for each of the managed agents.
- an `agent.json` file containing a dictionary representing your agent.
- a `prompt.yaml` file containing the prompt templates used by your agent.
- an `app.py` file providing a UI for your agent when it is exported to a Space with `agent.push_to_hub()`
- a `requirements.txt` containing the names of the modules used by your tool (as detected when inspecting its code)

####
step

< source >

( memory_step: ActionStep )

Perform one step in the ReAct framework: the agent thinks, acts, and observes the result. Returns either None if the step is not final, or the final answer.

####
to_dict

< source >

( ) →
`dict`

Returns

`dict`

Dictionary representation of the agent.

Convert the agent to a dictionary representation.

####
visualize

< source >

( )

Creates a rich tree visualization of the agent’s structure.

####
write_memory_to_messages

< source >

( summary_mode: bool = False )

Reads past llm_outputs, actions, and observations or errors from the memory into a series of messages that can be used as input to the LLM. Adds a number of keywords (such as PLAN, error, etc) to help the LLM.

###
class smolagents.CodeAgent

< source >

( tools: list model: Model prompt_templates: smolagents.agents.PromptTemplates | None = None additional_authorized_imports: list[str] | None = None planning_interval: int | None = None executor_type: typing.Literal['local', 'e2b', 'modal', 'docker', 'wasm'] = 'local' executor_kwargs: dict[str, typing.Any] | None = None max_print_outputs_length: int | None = None stream_outputs: bool = False use_structured_outputs_internally: bool = False code_block_tags: str | tuple[str, str] | None = None **kwargs )

Parameters

- tools (`list[Tool]`) — Tools that the agent can use.
- model (`Model`) — Model that will generate the agent’s actions.
- prompt_templates (PromptTemplates, optional) — Prompt templates.
- additional_authorized_imports (`list[str]`, optional) — Additional authorized imports for the agent.
- planning_interval (`int`, optional) — Interval at which the agent will run a planning step.
- executor_type (`Literal["local", "e2b", "modal", "docker", "wasm"]`, default `"local"`) — Type of code executor.
- executor_kwargs (`dict`, optional) — Additional arguments to pass to initialize the executor.
- max_print_outputs_length (`int`, optional) — Maximum length of the print outputs.
- stream_outputs (`bool`, optional, default `False`) — Whether to stream outputs during execution.
- use_structured_outputs_internally (`bool`, default `False`) — Whether to use structured generation at each action step: improves performance for many models.

  Added in 1.17.0

- code_block_tags (`tuple[str, str]` | `Literal["markdown"]`, optional) — Opening and closing tags for code blocks (regex strings). Pass a custom tuple, or pass ‘markdown’ to use (”`(?:python|py)", "\n`”), leave empty to use (”`”, ”`”).
- **kwargs — Additional keyword arguments.

In this agent, the tool calls will be formulated by the LLM in code format, then parsed and executed.

####
cleanup

< source >

( )

Clean up resources used by the agent, such as the remote Python executor.

####
from_dict

< source >

( agent_dict: dict **kwargs ) →
`CodeAgent`

Parameters

- agent_dict (`dict[str, Any]`) — Dictionary representation of the agent.
- **kwargs — Additional keyword arguments that will override agent_dict values.

Returns

`CodeAgent`

Instance of the CodeAgent class.

Create CodeAgent from a dictionary representation.

###
class smolagents.ToolCallingAgent

< source >

( tools: list model: Model prompt_templates: smolagents.agents.PromptTemplates | None = None planning_interval: int | None = None stream_outputs: bool = False max_tool_threads: int | None = None **kwargs )

Parameters

- tools (`list[Tool]`) — Tools that the agent can use.
- model (`Model`) — Model that will generate the agent’s actions.
- prompt_templates (PromptTemplates, optional) — Prompt templates.
- planning_interval (`int`, optional) — Interval at which the agent will run a planning step.
- stream_outputs (`bool`, optional, default `False`) — Whether to stream outputs during execution.
- max_tool_threads (`int`, optional) — Maximum number of threads for parallel tool calls. Higher values increase concurrency but resource usage as well. Defaults to `ThreadPoolExecutor`’s default.
- **kwargs — Additional keyword arguments.

This agent uses JSON-like tool calls, using method `model.get_tool_call` to leverage the LLM engine’s tool calling capabilities.

####
execute_tool_call

< source >

( tool_name: str arguments: dict[str, str] | str )

Parameters

- tool_name (`str`) — Name of the tool or managed agent to execute.
- arguments (dict[str, str] | str) — Arguments passed to the tool call.

Execute a tool or managed agent with the provided arguments.

The arguments are replaced with the actual values from the state if they refer to state variables.

####
process_tool_calls

< source >

( chat_message: ChatMessage memory_step: ActionStep ) →
`ToolCall | ToolOutput`

Parameters

- chat_message (`ChatMessage`) — Chat message containing tool calls from the model.
- memory_step (`ActionStep)` — Memory ActionStep to update with results.

Yields

`ToolCall | ToolOutput`

Process tool calls from the model output and update agent memory.

###
stream_to_gradio

####
smolagents.stream_to_gradio

< source >

( agent task: str task_images: list | None = None reset_agent_memory: bool = False additional_args: dict | None = None )

Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages.

###
GradioUI

> You must have `gradio` installed to use the UI. Please run `pip install 'smolagents[gradio]'` if it’s not the case.

###
class smolagents.GradioUI

< source >

( agent: MultiStepAgent file_upload_folder: str | None = None reset_agent_memory: bool = False )

Parameters

- agent (MultiStepAgent) — The agent to interact with.
- file_upload_folder (`str`, optional) — The folder where uploaded files will be saved. If not provided, file uploads are disabled.
- reset_agent_memory (`bool`, optional, defaults to `False`) — Whether to reset the agent’s memory at the start of each interaction. If `True`, the agent will not remember previous interactions.

Raises

`ModuleNotFoundError`

- `ModuleNotFoundError` — If the `gradio` extra is not installed.

Gradio interface for interacting with a MultiStepAgent.

This class provides a web interface to interact with the agent in real-time, allowing users to submit prompts, upload files, and receive responses in a chat-like format. It can reset the agent’s memory at the start of each interaction if desired. It supports file uploads, which are saved to a specified folder. It uses the `gradio.Chatbot` component to display the conversation history. This class requires the `gradio` extra to be installed: `pip install 'smolagents[gradio]'`.

Example:

    from smolagents import CodeAgent, GradioUI, InferenceClientModel

    model = InferenceClientModel(model_id="meta-llama/Meta-Llama-3.1-8B-Instruct")
    agent = CodeAgent(tools=[], model=model)
    gradio_ui = GradioUI(agent, file_upload_folder="uploads", reset_agent_memory=True)
    gradio_ui.launch()

####
launch

< source >

( share: bool = True **kwargs )

Parameters

- share (`bool`, defaults to `True`) — Whether to share the app publicly.
- **kwargs — Additional keyword arguments to pass to the Gradio launch method.

Launch the Gradio app with the agent interface.

####
upload_file

< source >

( file file_uploads_log allowed_file_types = None )

Parameters

- file (`gradio.File`) — The uploaded file.
- file_uploads_log (`list`) — A list to log uploaded files.
- allowed_file_types (`list`, optional) — List of allowed file extensions. Defaults to [“.pdf”, “.docx”, “.txt”].

Upload a file and add it to the list of uploaded files in the session state.

The file is saved to the `self.file_upload_folder` folder. If the file type is not allowed, it returns a message indicating the disallowed file type.

##
Prompts

###
class smolagents.PromptTemplates

< source >

( )

Parameters

- system_prompt (`str`) — System prompt.
- planning (PlanningPromptTemplate) — Planning prompt templates.
- managed_agent (ManagedAgentPromptTemplate) — Managed agent prompt templates.
- final_answer (FinalAnswerPromptTemplate) — Final answer prompt templates.

Prompt templates for the agent.

###
class smolagents.PlanningPromptTemplate

< source >

( )

Parameters

- plan (`str`) — Initial plan prompt.
- update_plan_pre_messages (`str`) — Update plan pre-messages prompt.
- update_plan_post_messages (`str`) — Update plan post-messages prompt.

Prompt templates for the planning step.

###
class smolagents.ManagedAgentPromptTemplate

< source >

( )

Parameters

- task (`str`) — Task prompt.
- report (`str`) — Report prompt.

Prompt templates for the managed agent.

###
class smolagents.FinalAnswerPromptTemplate

< source >

( )

Parameters

- pre_messages (`str`) — Pre-messages prompt.
- post_messages (`str`) — Post-messages prompt.

Prompt templates for the final answer.

##
Memory

Smolagents use memory to store information across multiple steps.

###
class smolagents.AgentMemory

< source >

( system_prompt: str )

Parameters

- system_prompt (`str`) — System prompt for the agent, which sets the context and instructions for the agent’s behavior.

Memory for the agent, containing the system prompt and all steps taken by the agent.

This class is used to store the agent’s steps, including tasks, actions, and planning steps. It allows for resetting the memory, retrieving succinct or full step information, and replaying the agent’s steps.

Attributes:

- system_prompt (`SystemPromptStep`) — System prompt step for the agent.
- steps (`list[TaskStep | ActionStep | PlanningStep]`) — List of steps taken by the agent, which can include tasks, actions, and planning steps.

####
get_full_steps

< source >

( )

Return a full representation of the agent’s steps, including model input messages.

####
get_succinct_steps

< source >

( )

Return a succinct representation of the agent’s steps, excluding model input messages.

####
replay

< source >

( logger: AgentLogger detailed: bool = False )

Parameters

- logger (`AgentLogger`) — The logger to print replay logs to.
- detailed (`bool`, default `False`) — If True, also displays the memory at each step. Defaults to False. Careful: will increase log length exponentially. Use only for debugging.

Prints a pretty replay of the agent’s steps.

####
reset

< source >

( )

Reset the agent’s memory, clearing all steps and keeping the system prompt.

####
return_full_code

< source >

( )

Returns all code actions from the agent’s steps, concatenated as a single script.

##
Python code executors

###
class smolagents.PythonExecutor

< source >

( )

###
Local Python executor

###
class smolagents.LocalPythonExecutor

< source >

( additional_authorized_imports: list max_print_outputs_length: int | None = None additional_functions: dict[str, collections.abc.Callable] | None = None )

Parameters

- additional_authorized_imports (`list[str]`) — Additional authorized imports for the executor.
- max_print_outputs_length (`int`, defaults to `DEFAULT_MAX_LEN_OUTPUT=50_000`) — Maximum length of the print outputs.
- additional_functions (`dict[str, Callable]`, optional) — Additional Python functions to be added to the executor.

Executor of Python code in a local environment.

This executor evaluates Python code with restricted access to imports and built-in functions, making it suitable for running untrusted code. It maintains state between executions, allows for custom tools and functions to be made available to the code, and captures print outputs separately from return values.

###
Remote Python executors

###
class smolagents.remote_executors.RemotePythonExecutor

< source >

( additional_imports: list logger )

####
run_code_raise_errors

< source >

( code: str )

Execute code, return the result and output, also determining if the result is the final answer.

####
send_variables

< source >

( variables: dict )

Send variables to the kernel namespace using pickle.

####
E2BExecutor

###
class smolagents.E2BExecutor

< source >

( additional_imports: list logger **kwargs )

Parameters

- additional_imports (`list[str]`) — Additional imports to install.
- logger (`Logger`) — Logger to use.
- **kwargs — Additional arguments to pass to the E2B Sandbox.

Executes Python code using E2B.

####
cleanup

< source >

( )

Clean up the E2B sandbox and resources.

####
ModalExecutor

###
class smolagents.ModalExecutor

< source >

( additional_imports: list logger app_name: str = 'smolagent-executor' port: int = 8888 create_kwargs: typing.Optional[dict] = None )

Parameters

- additional_imports — Additional imports to install.
- logger (`Logger`) — Logger to use for output and errors.
- app_name (`str`) — App name.
- port (`int`) — Port for jupyter to bind to.
- create_kwargs (`dict`, optional) — Keyword arguments to pass to creating the sandbox. See `modal.Sandbox.create` docs for all the keyword arguments.

Executes Python code using Modal.

####
delete

< source >

( )

Ensure cleanup on deletion.

####
DockerExecutor

###
class smolagents.DockerExecutor

< source >

( additional_imports: list logger host: str = '127.0.0.1' port: int = 8888 image_name: str = 'jupyter-kernel' build_new_image: bool = True container_run_kwargs: dict[str, typing.Any] | None = None dockerfile_content: str | None = None )

Executes Python code using Jupyter Kernel Gateway in a Docker container.

####
cleanup

< source >

( )

Clean up the Docker container and resources.

####
delete

< source >

( )

Ensure cleanup on deletion.

####
WasmExecutor

###
class smolagents.WasmExecutor

< source >

( additional_imports: list logger deno_path: str = 'deno' deno_permissions: list[str] | None = None timeout: int = 60 )

Parameters

- additional_imports (`list[str]`) — Additional Python packages to install in the Pyodide environment.
- logger (`Logger`) — Logger to use for output and errors.
- deno_path (`str`, optional) — Path to the Deno executable. If not provided, will use “deno” from PATH.
- deno_permissions (`list[str]`, optional) — List of permissions to grant to the Deno runtime. Default is minimal permissions needed for execution.
- timeout (`int`, optional) — Timeout in seconds for code execution. Default is 60 seconds.

Remote Python code executor in a sandboxed WebAssembly environment powered by Pyodide and Deno.

This executor combines Deno’s secure runtime with Pyodide’s WebAssembly‑compiled Python interpreter to deliver s trong isolation guarantees while enabling full Python execution.

####
cleanup

< source >

( )

Clean up resources used by the executor.

####
delete

< source >

( )

Ensure cleanup on deletion.

####
install_packages

< source >

( additional_imports: list ) →
list[str]

Parameters

- additional_imports (`list[str]`) — Package names to install.

Returns

list[str]

Installed packages.

Install additional Python packages in the Pyodide environment.

####
run_code_raise_errors

< source >

( code: str ) →
`CodeOutput`

Parameters

- code (`str`) — Python code to execute.

Returns

`CodeOutput`

Code output containing the result, logs, and whether it is the final answer.

Execute Python code in the Pyodide environment and return the result.

< > Update on GitHub