When to reach for it
- APIs are inconsistent or unstable.
- Agents require simple tool contracts.
- Security or error logic must be encapsulated.
Wrap the messy API in a clean tool the model can call.
A wrapper translates a messy or unstable external API into a small, stable, agent-friendly tool contract — so the model sees one clean function while the adapter absorbs auth, pagination, error handling, and schema drift behind it.
In practiceAn HR agent wraps a 15-year-old SOAP payroll API in an adapter that exposes a single `get_employee_salary(id)` function, shielding the model from XML envelope details.
The model sees one small function. Everything awkward about the real API stays behind it.
1 · Design the contract
2 · On every call
| Concern | Without adapter | With adapter |
|---|---|---|
| Request | The model writes an XML envelope that includes a session token | get_employee_salary("E-1042") |
| Result | 60 lines of XML, the salary in cents inside a nested tag | {"salary": 5400.0, "currency": "EUR"} |
| Error | A fault code of the payroll system | {"error": "employee_not_found"} |
| API change | Every prompt that names the old field breaks | One line in the adapter changes |
def get_employee_salary(employee_id: str) -> dict:
"""Return the current gross monthly salary of one employee."""
# auth stays in here
envelope = SOAP_TEMPLATE.format(token=session_token(), id=employee_id)
try:
xml = post(PAYROLL_URL, envelope, timeout=10)
except Timeout:
# a short error the model can act on
return {"error": "payroll_unavailable"}
node = parse(xml).find(".//GrossMonthlyCents")
if node is None:
return {"error": "employee_not_found"}
# one stable shape
return {"salary": int(node.text) / 100, "currency": "EUR"}When to reach for it
When it backfires
The wrapper swallows a 429 or a partial result and returns a clean shape. The agent reasons against the clean shape and acts as if the call succeeded.
Countermeasure
Pass through structured errors with category (rate limit, partial, fatal). Let the agent or the harness decide how to react — never let the adapter quietly normalize away signal.
The wrapped API changes a field or its semantics. The adapter has no contract test, so the drift flows straight to the model, which reasons against a contract that quietly moved.
Countermeasure
Contract-test the adapter against the live API and version it; pin the agent to an adapter version so an upstream change fails the test rather than the run.
Keep going
Search patterns, frameworks, and pages.