Skip to content

Python Code as an agent's tool

An agent is good at deciding what to do, but it should not work out by hand calculations or rules that must always come out the same. Give it that part as a tool written in Python: the agent decides when to use it and with what data, and your code does the calculation.

1. Connect it to the agent

Connect the Tool output of Python Code to the agent’s Tools input. Once it is connected, the Processed Data output is hidden.

2. Describe it in the Tool tab

FieldWhat to write
Tool NameThe name the agent sees the tool by, for example calculate_quote.
Tool DescriptionWhat it is for and when to use it. The agent decides based on this text.
Tool Input SchemaThe data the agent must send. Press Edit Argument Schema: for each item, its name, its type, a description, and whether it is required.

3. Read the data in args

The data the agent sends arrives in args, with the names you defined. What you return is the answer the agent gets.

prices = {"drill": 85.0, "ladder": 140.0}
price = prices[args["product"]]
units = int(args["units"])
discount = 0.10 if units >= 100 else 0.0
return {"total_eur": round(units * price * (1 - discount), 2), "discount": discount}

If your code fails, the agent gets the error instead of the result, and can tell the user or try again.

Example

Central Hardware’s sales assistant works out quotes with the calculate_quote tool, which applies the price list and the volume discount. Its instructions say: “To work out a quote always use the tool, never calculate it yourself”. When a customer asks how much 120 drills would cost, the agent calls the tool with the product and the units, and answers with the total the code worked out.

The same tool works for a voice agent or can be published to other assistants with MCP.