LLM Providers & APIs

Direct model providers and unified gateways for routing, caching, and observability.

Every agent needs a model behind it. This chapter covers both the direct providers and the gateway layer that lets you avoid hardcoding your whole stack to one vendor's pricing and uptime.

Start with direct providers when you know your primary model (OpenAI, Claude, Gemini, or a specific open-weight family). Add a gateway when you need one integration surface for many models, centralized billing, or fallbacks — common once you ship beyond a single-provider demo.

Direct Model Providers

Unified Gateways & Routers

What Is It?

Every agent needs a model provider behind the loop.

Direct APIs give first-party features; gateways unify routing, caching, and keys.

Child pages compare vendors and gateway features.

Why Should I Care?

Token spend and rate limits dominate agent unit economics.

Vendor outages happen — abstraction layers save sleep.

Real World Analogy

Direct providers are calling the chef; gateways are food delivery apps that let you reorder from many restaurants with one account.

Visual Mental Model

text
app -> gateway -> OpenAI / Anthropic / local
  app -> provider API direct

Beginner Explanation

This hub splits direct providers (OpenAI, Anthropic, Google, open weights) and gateways (OpenRouter, LiteLLM, Portkey). You need both maps for cost and failover.

Smallest Possible Example

python
MODEL = "gpt-4.1-mini"  # abstract name; swap via gateway

Common Mistakes

❌ Hardcoding one model string everywhere.

Use config/env so you can swap models without refactors.

❌ Ignoring rate limits on tool-heavy loops.

Each step may be another completion — budget tokens.

❌ Storing provider keys in frontend bundles.

Keys belong on server-side runners only.

Real Project Example

OpenRouter and LiteLLM appear in startups mixing cheap and premium models.

Helicone and Portkey add observability on top of gateways.

Build This Mini Project

Move model name and base URL to environment variables; document fallback model in README.

Interview Question

Why use an LLM gateway?

Unified API across providers, centralized keys, routing/fallbacks, caching, and spend controls — reduces lock-in and simplifies ops.

Key Takeaways

  • Hub covers direct + gateway paths.
  • Configure models externally.
  • Plan for rate limits and cost.
  • Never expose keys client-side.

YouTube learning

Core concepts from video walkthroughs:

Threads in the same domain as this chapter — go deeper on iHateReading without leaving the roadmap.

OpenAI DevDay 2026 for builders cover

OpenAI DevDay 2026 for builders

Dots distribution, Sign in with ChatGPT, and the trigger–decision–action loop.

Jev, explained like you're five (then like you're building with it) cover

Jev, explained like you're five (then like you're building with it)

System One decision models for fast, typed judgments inside software.

AI Agent cover

AI Agent

What agents are and how they differ from a single chat completion.

Python AI agent SDKs in 2026: OpenAI vs Pydantic-AI vs smolagents vs Strands cover

Python AI agent SDKs in 2026: OpenAI vs Pydantic-AI vs smolagents vs Strands

Same three-tool agent in four frameworks — pick by execution model and testing needs.