By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A tool is a function with a contract, and that contract is the only part of the function the model ever sees. It reads the name, the docstring and the argument schema, then chooses a call. If the contract is vague, the model fills the gap with a confident guess - and a guess that validates against a loose schema reaches production.
The tools lesson introduced this idea; this lab makes it testable. You will write the contracts Harbor Helper actually needs, run a deterministic checker that reads them the way the model does, and fix a deliberately broken tool until the checker is green. There is no model call, no API key and no database: a schema is just data, so the test is ordinary Python.
Before the hands-on, name the decisions. Every tool in this path makes the same five, and each one has a cheap test.
snake_case: lookup_order, not orders. A name the model would use as a button label is easier to choose correctly.order_id without a format invites a guess.required only for what you cannot default, and additionalProperties: false so invented keys fail loudly.{'ok': False, 'error': 'invalid_order_id', 'retryable': False} - instead of raising through the loop, so the model can recover or ask a better question.idempotency_key. A retry with the same key must find the existing record, not create a second one.The snippet below defines three Harbor Helper tool contracts and a checker that applies the five decisions. lookup_order and queue_for_approval are complete on purpose. search_docs has three defects: run the checker, fix them, and re-run the same script until the last line reads contract checks: 16/16 pass.
search_docs.Use when or Use for.additionalProperties: false) and give max_results a description with its default and cap.search_docs: pass and contract checks: 16/16 pass.issue_refund on paper: decide whether it can be model-reachable, which arguments it needs, and why it must carry an idempotency key even if it is only ever queued."""Deterministic schema checks for the Harbor Helper tool contracts.
No model and no API key: this test reads the same material the model will
read - the name, the docstring and the argument schema.
"""
import re
TOOL_SCHEMAS = {
"lookup_order": {
"description": (
"Look up one order by its store order id. Use when the customer "
"asks about the status, contents or delivery of a known order."
),
"parameters": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"pattern": r"^ORD-\d{4}$",
"description": "Store order id, for example ORD-4521.",
}
},
"required": ["order_id"],
"additionalProperties": False,
},
},
"queue_for_approval": {
"description": (
"Queue a proposed write for a human decision. Use for refunds, "
"replacements or address changes; this tool never executes the "
"write."
),
"parameters": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["refund", "replacement", "address_change"],
"description": "The write a human is being asked to "
"approve.",
},
"order_id": {
"type": "string",
"description": "Store order id.",
},
"idempotency_key": {
"type": "string",
"description": "Stable key reused if this request is "
"retried.",
},
},
"required": ["action", "order_id", "idempotency_key"],
"additionalProperties": False,
},
},
"search_docs": {
"description": "Search the help centre.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search terms.",
},
"max_results": {"type": "integer"},
},
"required": ["query"],
"additionalProperties": True,
},
},
}
def schema_checks(name, schema):
params = schema["parameters"]
props = params["properties"]
checks = {
"snake_case name": re.fullmatch(r"[a-z][a-z0-9_]*", name) is not None,
"description says when": (
"use when" in schema["description"].lower()
or "use for" in schema["description"].lower()
),
"closed object": params["additionalProperties"] is False,
"required args exist": all(arg in props for arg in params["required"]),
"args documented": all(
"description" in spec for spec in props.values()
),
}
if name == "queue_for_approval":
checks["idempotency required"] = (
"idempotency_key" in params["required"]
)
return checks
total = passed = 0
for name, schema in TOOL_SCHEMAS.items():
checks = schema_checks(name, schema)
total += len(checks)
passed += sum(checks.values())
failed = [label for label, ok in checks.items() if not ok]
print(f"{name}: {'pass' if not failed else 'fail ' + ', '.join(failed)}")
print(f"contract checks: {passed}/{total} pass")Read the failure line first: three labels, three edits. That is the point of the exercise - schema problems are cheap to find when the contract is a data structure and a deterministic test, and expensive to find when they are a production ticket.
snake_case name - the name is an instruction to the model, so it must be shaped like one.description says when - the docstring has to include a usage clause, not just a noun phrase.closed object - additionalProperties: false turns an invented argument into a schema error instead of a silent typo.required args exist and args documented - every required argument is declared and described; an undocumented parameter is a parameter the model guesses.idempotency required - the write-proposal tool must require a stable key, or a retry can duplicate the proposal.The tools lesson said tool errors are messages, not exceptions. The schema perspective is sharper: the error is part of the interface, so it needs a shape and a test.
invalid_order_id, not_found, forbidden, rate_limited - a stable string the model and your tests can branch on.Order ids look like ORD-4521. Keep it short and data-only.ToolMessage. The model repeats what it reads, and so do the logs.A tool that only reads is safe to retry: the same input gives the same answer. A tool that proposes a write is not, because the loop, a user retry or a network timeout can send the same call twice. The fix lives in the schema: the call carries an idempotency_key that the queue and the executor use as the identity of the request.
The flow is proposal, decision, execution, and every step is keyed. The agent can only reach queue_for_approval, never the executor. The queue stores the exact action and arguments with the key. A duplicate proposal with the same key returns the existing record instead of adding a second refund, replacement or address change to a human's queue.
snake_case, and no two tools read alike.Key takeaways
A tool's schema is its public API: the model sees only the name, docstring and argument schema.
Say when to use a tool and show the identifier format; an undocumented argument is an invitation to guess.
Return structured error results with stable codes and honest hints instead of raising through the loop.
Require an idempotency key on every write proposal, and make a duplicate proposal return the existing record.
A deterministic schema test - closed arguments, documented parameters, required idempotency - is the cheapest guardrail you can ship.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.