C.W.K.
Stream
Lesson 01 of 07 · published

Adapter Pattern

~22 min · adapter, production

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The Adapter pattern is the foundation of a production agent architecture. By abstracting the LLM behind stream() → AsyncIterator[Chunk], you can swap backends, add fallbacks, and test without API calls.

Why Adapters?

  • Swappable backends — Switch between gpt-5.4, gpt-4.1, or local models without changing agent code.
  • Auth flexibility — API keys, OAuth tokens, or custom auth schemes.
  • Testing — Mock adapters return predetermined responses.
  • Fallback chains — Primary → secondary → tertiary model cascade.

Tool Bridge Mapping

The adapter + tool bridge pattern cleanly separates the LLM communication layer from the tool execution layer from the business logic.

Code

Production Adapter ABC sketch·python
# Map tool names to callables for the agent loop
TOOLS_MAP = {
    "get_weather": get_weather,
    "search_news": search_news,
    "query_database": query_database,
}

# Tool schemas for the model
TOOL_SCHEMAS = [
    {"type": "function", "name": name, "description": func.__doc__,
     "parameters": get_schema(func), "strict": True}
    for name, func in TOOLS_MAP.items()
]

External links

Exercise

List the leak points where 'provider' currently appears in your code. Group them as 'fundamental wire differences' (legitimate) vs 'prompt or tool variation' (bug). Plan one consolidation.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.