Adapter Pattern

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.

Level
04 · Production
Subdomain
Tool Integration
Also known as
Tool Adapter, API Wrapper, Integration Adapter
Trade-off
More stable agent interfaces are achieved against the need to maintain an additional code layer.
The mechanism

How it works.

The model sees one small function. Everything awkward about the real API stays behind it.

1 · Design the contract

  1. Pick what the agent needsOne question, one function
  2. Define a small schemaFew arguments, plain types
  3. Define the errorsA short list the model can act on

2 · On every call

  1. The model calls the functionWith the few arguments of the contract
  2. The adapter translatesAuthentication, request format, paging
  3. The adapter normalisesOne shape for results, one for errors
  4. The model gets a clean resultNo trace of the system behind it
Contract
The function as the model sees it: name, arguments, result shape and error list. It stays stable.
Normalisation
Turning whatever the API returns into the one shape the contract promises.
Schema drift
The API renames a field or changes a type. The adapter absorbs the change, and the contract stays as it was.
Walk it through

A real run, step by step.

TaskAn HR agent needs salaries from a payroll system that speaks SOAP. The same request, without and with an adapter:
Illustrative comparison.
ConcernWithout adapterWith adapter
RequestThe model writes an XML envelope that includes a session tokenget_employee_salary("E-1042")
Result60 lines of XML, the salary in cents inside a nested tag{"salary": 5400.0, "currency": "EUR"}
ErrorA fault code of the payroll system{"error": "employee_not_found"}
API changeEvery prompt that names the old field breaksOne line in the adapter changes
What this shows: the adapter removes three things from the model's context: the request format, the raw response and the credential. The model can no longer get the envelope wrong, and it never sees the token. The price is a layer of code that has to be tested and kept in step with the API.
In code

The pattern in code.

Plain Pythonpython
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"}
Fit

Use it, or not.

When to reach for it

  • APIs are inconsistent or unstable.
  • Agents require simple tool contracts.
  • Security or error logic must be encapsulated.

When it backfires

  • Native SDKs already provide suitable interfaces.
  • The adapter merely passes data without adding value.
  • The abstraction hides important semantics.
Pitfalls

Two ways this pattern will hurt you.

The adapter hides the failure mode it should surface

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 upstream changes and the adapter passes it through

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.

Framework support

Where Adapter Pattern is native.

LangGraphcustom toolsAdaptable
Google ADKcustom toolsAdaptable
AWS Strandscustom toolsAdaptable
Microsoft Agent Frameworkcustom toolsAdaptable
Sources

Where this pattern comes from.

  1. Design Patterns: Elements of Reusable Object-Oriented SoftwareIn the source list

Search

Search patterns, frameworks, and pages.