As we wrap up direct provider calls, let's look at the final category β routers and abstraction layers.
π§ What's the Difference?
Type | How It Works |
|---|---|
Router | A remote service you connect to β it forwards your request to whichever provider you choose (e.g. OpenRouter) |
Abstraction Layer | A framework running locally on your machine that translates a unified API into provider-specific calls (e.g. LiteLLM, LangChain) |
π OpenRouter in Action
OpenRouter is one of the most popular routers β and you may have already been using it throughout this lab.
Why use it?
π One API key for all providers
π³ Single billing account
π§ͺ Easy model switching β just change the model name
π Some models are completely free
βΆοΈ Create a new code cell and run it:
response = openrouter.chat.completions.create(model="z-ai/glm-4.5", messages=tell_a_joke)
display(Markdown(response.choices[0].message.content))Here we used GLM-4.5 β an open-source model from Chinese startup ZhipuAI, accessed entirely through OpenRouter.
π‘ All model pricing, performance benchmarks, and availability are listed on openrouter.ai
π§© LLM Abstraction Layers & Frameworks
π LangChain
π The most famous LLM framework β often the reason people take LLM engineering courses
ποΈ Heavyweight abstraction layer with many powerful built-in concepts
π Lets you swap between different model providers by importing different
langchain_<provider>packagesπ¦ Usage pattern:
βΆοΈ Create a new code cell and run it:
from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-5-mini") response = llm.invoke(tell_a_joke) display(Markdown(response.content))β οΈ Has a steep learning curve β there's a lot to learn before it feels natural
β Covered in depth in Later Sections
β‘ LiteLLM
πͺΆ Lightweight counterpart to LangChain β does what it says on the tin
π― Single unified interface to call any model from any provider
π§ͺ Usage pattern:
βΆοΈ Create a new code cell and run it:
from litellm import completion response = completion(model="openai/gpt-4.1", messages=tell_a_joke) reply = response.choices[0].message.content display(Markdown(reply))π Provider format:
provider/model-nameAWS Bedrock β
bedrock/...Azure β
azure/...Google Vertex β
vertex_ai/...
π° Automatically reports input tokens, output tokens, and cost per call
π Great for production systems β can track per-user token spend across models
πΈ API Cost Reality Check
Costs are quoted per million tokens β individual calls are tiny
Example: a full Hamlet (shakespearean play) (~53K tokens) call cost ~0.5 cents
Don't fear API costs for dev/testing, but do monitor unit economics at scale
LiteLLM makes tracking per-call costs trivially easy