Unit 07.01: Letting the model choose, within limits
An agent proposing a tool call is making a suggestion. The registry decides whether it happens.
Reject by name, with the real list attached
Only registered names run. Anything else is refused, and the refusal carries the available names.
The code checks four proposed calls against a registry of two.
from langchain_core.tools import tool
REGISTERED = {}
@tool
def read_account(account_id: str) -> str:
"""Read an account record."""
return f"record for {account_id}"
@tool
def check_policy(topic: str) -> str:
"""Look up a policy by topic."""
return f"policy text for {topic}"
REGISTERED = {t.name: t for t in (read_account, check_policy)}
PROPOSED = ["read_account", "check_policy", "issue_refund", "send_email"]
for name in PROPOSED:
if name in REGISTERED:
print(f"ALLOW {name}")
else:
print(f"REJECT {name} -- not registered. Available: {sorted(REGISTERED)}")
invented = [n for n in PROPOSED if n not in REGISTERED]
print(f"\n{len(invented)} invented tool names: {invented}")
# The model proposes; the registry disposes. Include the real names in the
# rejection so the retry has information -- a bare refusal gets you a second
# plausible invention and another paid call.
issue_refund and send_email are both entirely plausible tools for this agent to want, which is exactly why it invented them - its goal implies capabilities its tool list does not contain.
Including the real names in the rejection is what makes the retry useful. A bare refusal gets a second plausible invention and another paid call; a refusal listing read_account and check_policy gets a call it can actually serve.
The mistake this prevents
The mistake is reading tool invention as a model defect to be prompted away. A high invention rate means the goal exceeds the tools, and the fix is to narrow the goal or add the tool deliberately - not to add another sentence telling the model which tools exist.
Takeaway
Enforce the registry and put the real tool names in every rejection. A high invention rate is a signal about your design, not about the model.
