Tools

First, read our introduction to tools.

Tools allow enhancing an agent's capabilities by allowing it to call external functions. Tools enable the creation of agents that can perform actions, retrieve information, and make decisions based on real-time data.

Defining custom tools

Custom tools are defined as regular python functions, and can be async or sync.

# Sync tool
def get_current_time(timezone: Annotated[str, "The timezone to get the current time in. e-g Europe/Paris"]) -> str:
    """Return the current time in the given timezone in iso format"""
    return datetime.now(ZoneInfo(timezone)).isoformat()

# Tools can also be async
async def get_latest_pip_version(package_name: Annotated[str, "The name of the pip package to check"]) -> str:
    """Fetch the latest version of a pip package from PyPI"""
    url = f"https://pypi.org/pypi/{package_name}/json"
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        response.raise_for_status()
        data = response.json()
        return data['info']['version']

To use the tool, add the function to the tools list in the @workflowai.agent decorator.

If an agent has access to tools, and the model deems that tools are needed for a particular run, the agent will:

  • call all tools in parallel

  • wait for all tools to complete

  • reply to the run with the tool outputs

  • continue with the next step of the run, and re-execute tools if needed

  • ...

  • until either no tool calls are requested, the max iteration (10 by default) or the agent has run to completion

The default maximum number of tool call iterations (turns) is 10. You can override this limit by passing the max_turns argument when calling the agent's run method:

It's important to understand that there are actually two runs created in a single agent run call:

  • the first run returns an empty output with a tool call request with a timezone

  • the second run returns the current time in the given timezone

Only the last run is returned to the caller.

Another example:

You can not directly use the web Playground to test custom tools, since the tools execution is done through your code.

Hosted tools

WorkflowAI hosts a few tools:

  • @browser-text allows fetching the content of a web page (text-only)

  • @google-search allows performing a web search using Google's search API

  • @perplexity-sonar-pro allows performing a web search using Perplexity's Sonar Pro model

Hosted tools tend to be faster because there is no back and forth between the client and the WorkflowAI API. Instead, if a tool call is needed, the WorkflowAI API will call it within a single request.

A single run will be created for all tool iterations.

To use a tool, simply add it's handles to the instructions (the function docstring):

Last updated