Describe and check tools so the agent uses them well
In this task you'll write tool descriptions the model can act on, and reject bad arguments before a tool runs.
A model only knows a tool through its name, description and input schema (a JSON Schema of its arguments). A vague description means the tool is ignored or used for the wrong job. Say what the tool does, when to use it and what it returns. Use an enum where only certain values are valid, such as service names.
Never trust the arguments a model sends. Validate them against the schema before running the tool: required fields present, no unknown fields, values from the allowed list. When a check fails, don't crash: send the error back to the model as the tool result, so it can try again or tell the user.
Example. Harbourline Design's agent kept restarting the wrong site because its tool took any text as a site name. Adding an enum of real site names, and returning 'site must be one of ...' when the model guessed, stopped the wrong restarts the same day.
Before you start
You need: Python 3.10 or newer (on Windows, use py where the steps say python). No AI account: the practice model runs on your computer. The agents folder from the first task. Time: 25 minutes.
Where to look
- agent.py
- TOOLS
- validate()
Steps
- Run
python agent.py "Why is shop-api slow? Look for errors and warnings.". The agent reads the logs and quotes the pool error. - In agent.py, change the read_logs description to just
Helper.and run the same request. The agent finds no tool that fits and gives up.The model can't use a tool it can't understand. - Put the good description back.
- Add the
Sschema with an enum of service names andvalidate()from Commands, and useSfor both tools. - In
run(), call validate before running a tool and send any error back as the tool result (see Commands). - Run
python agent.py "Check status of web-frontend". The step shows the call rejected, and the answer says which services exist.The tool never ran with a made-up name.
Commands
# Schema with the only valid service names, used by both tools
S = {"type": "object", "properties": {"service": {"type": "string", "enum": sorted(SERVICES)}}, "required": ["service"]}
def validate(tool, args):
schema = next(t["input_schema"] for t in TOOLS if t["name"] == tool)
for k in schema.get("required", []):
if k not in args: return f"missing argument {k}"
for k, v in args.items():
spec = schema["properties"].get(k)
if spec is None: return f"unknown argument {k}"
if "enum" in spec and v not in spec["enum"]: return f"{k} must be one of {spec['enum']}"
return None
# In run(), replace the line that runs the tool with:
tool, args = reply["name"], reply["input"]
error = validate(tool, args) if tool in RUN else f"no tool called {tool}"
if error: result = f"error: {error}"
else: result = RUN[tool](**args)
python agent.py "Check status of web-frontend"
Watch out for
- Cost
- Free.
- Security
- Validation is a safety check, not only tidiness: it stops a model, or text that fooled it, from passing paths, commands or names you never meant to allow.
- Common mistakes
- Descriptions that say what the code is ('wrapper for the API') instead of when to use it, and tools that accept free text where a list of values would do.
Clean up
Keep the agents folder for the next tasks in this section; delete it at the end.
Words to know
- Tool description
- The text the model reads to decide when to use a tool.
- Input schema
- A JSON Schema listing a tool's arguments and their types.
- enum
- A schema rule listing the only values an argument may take.
- Validation
- Checking arguments before a tool runs.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On