Architecture, Implementation, and Production Engineering
Introduction
- References
Chapter 1: The Problem Space
- Direct Provider Integration Anti-Patterns
- The Cost of Vendor Lock-In and Fragmentation
- What Abstraction Should and Should Not Do
- Defining the Success Criteria
- A Realistic Scope for Provider Agnostic Design
- References
Chapter 2: Architectural Principles and Requirements
- Stability Over Novelty in API Design
- Capability Preservation Versus Uniformity
- Explicit Error Semantics and Classification
- Idempotency and Request Safety Guarantees
- The Provider Adapter Pattern as Core Abstraction
- Observability as a First-Class Requirement
- References
Chapter 3: Project Structure and Foundation
- Project Layout and Package Design
- Dependency Declaration and Version Pinning
- Core Type Definitions and Error Taxonomy
- Configuration and Environment Management
- Logger Setup and Structured Logging
- Initial Unit Test Infrastructure
- Summary
- References
Chapter 4: Unified Request and Response Model
- Message Types and Conversation Representation
- The LlmRequest Protocol
- The LlmResponse Protocol and Metadata
- Token Usage Accounting
- Handling Provider-Specific Response Fields
- Round-Trip Serialization and Validation
- Summary
- References
Chapter 5: Provider Adapter Interface
- The ProviderAdapter Protocol Design
- Capability Negotiation and Discovery
- Error Mapping and Translation
- Reference Implementation: OpenAI Adapter
- Testing the Adapter Contract
- Designing for New Provider Integration
- Summary
- References
Chapter 6: Multiple Provider Integrations
- Anthropic Adapter: Different Message Semantics
- Google Vertex AI Adapter: Complex Authentication and Transport
- Together AI Adapter: Community Model Access Patterns
- Normalizing Divergent Streaming Protocols
- Model Registry and Capability Matrices
- Provider Selection and Routing Logic
- Summary
- References
Chapter 7: Authentication and Configuration
- Credential Abstraction and Provider Binding
- Multiple Authentication Strategies per Provider
- Secrets Management Integration Patterns
- Configuration Validation and Fail-Fast
- Per-Request Override Mechanics
- Security Review: Attack Surface and Mitigations
- Summary
- References
Chapter 8: Execution Modes: Sync, Async, and Streaming
- Async-First Design with Sync Compatibility
- Streaming Response Infrastructure
- Server-Sent Events Parsing and Normalization
- Partial Response Accumulation and Chunking
- Cancellation and Timeout Propagation
- Backpressure and Flow Control in Streaming
- Summary
- References
Chapter 9: Structured Outputs
- Schema Definition and Pydantic Integration
- Provider-Specific Structured Output Features
- Schema Translation and Compatibility Layer
- Response Validation and Error Recovery
- Fallback Strategies for Unsupported Providers
- Performance Considerations for Schema Validation
- Summary
- References
Chapter 10: Tool Calling and Function Integration
- Tool Definition Abstraction
- Provider-Specific Tool Calling Protocols
- Multi-Tool Selection and Parallel Calls
- Tool Result Formatting and Round-Tripping
- Error Handling in Tool Execution Loops
- Streaming with Tool Calling
- Summary
- References
Chapter 11: Multimodal Input Handling
- Content Block Abstraction for Mixed Media
- Image Input Normalization Across Providers
- File Handling, Encoding, and Size Limits
- Vision Model Capability Discovery
- Composing Multimodal Requests
- Testing Multimodal Integrations
- Summary
- References
Chapter 12: Model Routing and Selection
- Routing Strategies and Decision Logic
- Cost-Aware Model Selection
- Capability-Based Routing
- A/B Testing and Traffic Splitting
- Dynamic Routing with Feedback Loops
- Routing as an Extensible Plugin System
- Summary
- References
Chapter 13: Retry Semantics and Transient Failure Handling
- Transient Versus Permanent Error Classification
- Retryable HTTP Status Codes and Provider Signals
- Exponential Backoff with Jitter
- Idempotency Keys and Duplicate Prevention
- Per-Operation Retry Policies
- Testing Retry Behavior Under Failure
- Summary
- References
Chapter 14: Circuit Breaking and Bulkhead Patterns
- Circuit Breaker State Machine Design
- Failure Rate and Half-Open Recovery Logic
- Provider-Specific Versus Global Circuit Breakers
- Bulkhead Isolation for Concurrency Control
- Integration with the Request Pipeline
- Monitoring Circuit Breaker State
- Summary
- References
Chapter 15: Fallback Strategies and Degradation
- Fallback Chain Design and Ordering
- Capability-Aware Fallback Selection
- Silent Versus Explicit Degradation
- Testing Fallback Behavior
- Cost Implications of Fallback Chains
- Real Incident: Provider Outage Recovery
- Summary
- References
Chapter 16: Observability and Distributed Tracing
- Structured Logging and Request Correlation
- Metrics: Latency, Costs, Errors, Saturation
- OpenTelemetry Integration and Trace Propagation
- Prompt and Response Logging with Redaction
- Debugging Production Issues with Traces
- Alerting on Key Reliability Indicators
- Summary
- References
Chapter 17: Comprehensive Testing Strategy
- Unit Testing with Mocked Providers
- Provider Contract Tests
- Integration Tests with Real APIs
- Fault Injection and Chaos Testing
- Concurrency and Race Condition Testing
- Performance Benchmarks and Regression Detection
- Summary
- References
Chapter 18: Security Engineering
- Secrets Management and Credential Security
- Prompt Injection Prevention
- Output Validation
- Authentication and Authorization for the Library
- Network Security
- OWASP LLM Top 10
- Summary
- References
Chapter 19: Token Counting and Cost Management
- Tokenization Fundamentals
- Tiktoken Integration for OpenAI Models
- Anthropic Tokenizer
- Token Counter Factory
- Cost Calculation
- Budget Management
- Summary
- References
Chapter 20: Performance Optimization
- Connection Pooling and HTTP Client Management
- HTTP/2 Performance Benefits
- Streaming Performance
- Caching Strategies
- Profiling and Bottleneck Identification
- Summary
- References
Chapter 21: API Versioning and Backward Compatibility
- Semantic Versioning Strategy
- Deprecation Patterns
- API Stability Guarantees
- Provider API Versioning
- Migration Guide
- Summary
- References
Chapter 22: Production Case Studies and Incident Analyses
- Case Study 1: OpenAI Outage December 11 2024
- Case Study 2: Cost Spike from GPT-4 Usage
- Case Study 3: Tool Call Malformed Output
- Case Study 4: Streaming Response Incomplete Due to Network Drop
- Case Study 5: Rate Limit Exhaustion During Traffic Spike
- Case Study 6: Model Deprecation Impact
- Summary
- References
Chapter 23: Future Evolution and Emerging Challenges
- Model Aggregation and Routing at Scale
- Multimodal Expansion
- Agent Framework Integration
- Edge Deployment and Local Models
- Observability Standardization
- Standardization Efforts
- Summary
- References
Chapter 24: Architectural Synthesis and Reference Guide
- Complete Component Architecture
- Request Flow: Complete Sequence
- Unified Interface Summary
- Provider Adapter Interface
- Design Decisions Summary
- Deployment Architecture
- Performance Reference
- Summary