Common Tools#
AG2 ships with ready-made tools and toolkits that bundle related function tools into a single Toolkit. Unlike built-in provider tools, these run locally as regular Python functions and work with every provider.
FilesystemToolkit#
FilesystemToolkit gives an agent the ability to read, write, update, delete, and search files within a sandboxed directory. All paths are resolved relative to a configurable base_path, and a path-traversal guard prevents access outside it.
Available tools#
| Tool | Description |
|---|---|
read_file | Read the contents of a file |
write_file | Create or overwrite a file (creates parent directories automatically) |
update_file | Replace the first occurrence of a string in a file |
delete_file | Delete a file |
find_files | Search for files matching a glob pattern (supports recursive ** patterns) |
Read-only mode#
Pass read_only=True to expose only read_file and find_files:
Using individual tools#
Every tool is available as an attribute on the toolkit instance. You can pass individual tools to an agent instead of the whole set:
Using a temporary directory#
For throwaway workspaces, use tempfile.TemporaryDirectory so the directory and all its contents are automatically cleaned up when the context manager exits:
Tip
Prefer tempfile.TemporaryDirectory over hardcoded /tmp paths. It guarantees a unique directory per run and cleans up after itself, avoiding leftover files and collisions between concurrent executions.
Path safety#
All paths are resolved relative to base_path. Any attempt to escape the base directory (e.g. ../../etc/passwd) raises a PermissionError:
DuckDuckSearchTool#
DuckDuckSearchTool gives an agent the ability to search the web using DuckDuckGo. No API key is required.
Note
Requires the ddgs extra: pip install ag2[ddgs]
Configuration#
All parameters accept a Variable for dynamic values resolved at execution time.
PerplexitySearchToolkit#
PerplexitySearchToolkit gives an agent two related tools powered by Perplexity: raw web search via the Search API and LLM-grounded answers with citations via Sonar — sharing a single client.
Note
Requires the perplexity extra and an API key: pip install ag2[perplexity]
If api_key is omitted, the Perplexity SDK reads the PERPLEXITY_API_KEY environment variable automatically.
Tools#
| Tool | Description |
|---|---|
perplexity_search | Raw web search via the Search API — ranked title/url/snippet/date results, no LLM hop |
perplexity_answer | LLM-generated answer with citations via Sonar Chat Completions — also returns search results, citations, and optional images |
Picking a subset of tools#
Each tool is exposed as a factory method on the toolkit (toolkit.search(), toolkit.answer()). Call the method to get a ready-to-use tool, then pass only the ones you need to the agent:
Per-tool configuration#
Per-call parameters live on the factory methods, not on the toolkit itself:
HTTP and SDK options#
The toolkit constructor accepts options for the underlying httpx.AsyncClient and the Perplexity SDK client. Any extra keyword arguments are forwarded directly to AsyncPerplexity(...) (e.g. base_url, max_retries, default_headers):
Result#
Both tools return a PerplexitySearchResponse with these fields:
| Field | Description |
|---|---|
query | The original search query |
results | List of PerplexitySearchResult (title, url, snippet, date) |
content | LLM-generated answer (filled by perplexity_answer; empty for perplexity_search) |
citations | URLs the model cited inline (filled by perplexity_answer) |
images | List of PerplexityImageMeta when return_images=True on perplexity_answer |
When return_images=True, image URLs are also surfaced as ImageInput parts on the tool result so the next model turn receives them as proper image inputs.
Tip
Use perplexity_search when the agent only needs raw ranked URLs (cheaper, no LLM hop). Use perplexity_answer when a grounded answer with citations is helpful.
Tip
search_domain_filter on perplexity_answer is a Pro-tier feature on the Perplexity API; see usage tiers.
TavilySearchTool#
TavilySearchTool gives an agent advanced web search capabilities via the Tavily API. Results include relevance scores and optional LLM-generated answers, raw page content, and images.
Note
Requires the tavily extra and an API key: pip install ag2[tavily]
If api_key is omitted, Tavily reads the TAVILY_API_KEY environment variable automatically.
Configuration#
All search parameters accept a Variable for dynamic values resolved at execution time.
HTTP and SDK options#
The constructor also accepts options for the underlying httpx.AsyncClient and the Tavily SDK client. Any extra keyword arguments are forwarded directly to AsyncTavilyClient(...) (e.g. api_base_url, company_info_tags, project_id):
SandboxShellTool#
SandboxShellTool gives an agent the ability to run shell commands inside an environment you choose. With no argument it uses a LocalEnvironment with a temporary working directory that is cleaned up on process exit. See the Sandbox Shell page for the full guide.
Warning
A LocalEnvironment executes arbitrary shell commands on your machine. Use allowed, blocked, or readonly to restrict what the agent can run — or a DockerEnvironment / DaytonaEnvironment for real isolation.
The first argument is the environment; the backend (where commands run) is configured there. Passing nothing uses a temporary local directory.
Restricting commands#
Command policy lives on the tool: