New Tool Radar

AI Agents & Automation

How to Register a Python Function as a Tool on an AG2 Agent Pair

Register a Python function as a tool on an AG2 ConversableAgent pair so the assistant can call it and the user proxy runs it.

Schematic with three steps: Describe the function to the assistant; Register the same function for execution; Ask a question that needs the function.

In AG2, a function becomes a tool when it is registered in two places: the assistant agent gets the function description so the model can decide to call it, and the user proxy agent gets the function itself so it can execute the call. Register the same function on both sides with matching descriptions, or the assistant will call a function nobody can run. The system message should also tell the assistant to use only the functions it was given.

What you need

  • Python with the autogen package installed
  • A small Python function the agent should be able to call
  • An AG2 assistant agent and user proxy agent already created

Step 1: Describe the function to the assistant

Register the function on the assistant agent with a clear name and description of what it does and when to use it. The description is what the model reads to decide whether to call it.

Step 2: Register the same function for execution

Register the same function on the user proxy agent for execution, so the proxy runs it when the assistant calls it and returns the result to the conversation.

Step 3: Ask a question that needs the function and check the result

Ask the assistant a question that only the function can answer, and confirm the function ran and the answer used its result. If the assistant answers without calling it, the description is too vague or the system message does not point at the tools.

Worked example: synthetic function call

The table below is a synthetic example. It shows the expected result of a registered function; no code was run.

Check Expected Result
Assistant calls the function Yes Pass
Proxy runs it and returns result Yes Pass
Answer uses the result Yes Pass
Function called with right arguments Yes Pass

Expected result (UNTESTED). the assistant calls the function, the proxy runs it, and the answer uses the returned result. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • The function must be registered on both sides; a description without an executor, or an executor without a description, means the call fails.
  • Check the system message aligns with the function descriptions, or the model will ignore the tools.
  • A function the model can call is a permission you grant, so keep it to the one job the agent needs.

Sources

See all