Sub topic 7
What frameworks add, and what they cost
This chapter assumes you can write basic Python, have called a chat API before, and know the earlier chapters' material: SDK calls, structured output with Pydantic, tool calling, and a hand-written agent loop. Here you will weigh what a framework gives you against what it takes from you.
What a framework adds
Each earlier chapter ended with code you wrote by hand. A framework packages the common parts of that code. The gains fall into four groups.
- A common interface. One chat model class wraps many providers, so changing providers mostly means changing a class name.
- Prebuilt pieces. Document loaders, text splitters, vector store connectors, and retrievers save you from writing the same RAG plumbing again.
- Automation of loops. Validation retries, tool dispatch, and multi-step agent loops come built in.
- Shared structure. A team that uses one framework shares one vocabulary, and new hires find familiar patterns.
Here is the same call made through LangChain. It is shorter than the raw SDK version in one respect only: the provider is now a swappable part.
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
result = llm.invoke("Explain what an API client library is in one sentence.")
print(result.content)For a single call, this saves almost nothing. The gains grow with the number of steps, providers, and data sources in your app.
What frameworks cost
Every layer you add sits between your code and the HTTP request. That layer has a price.
| Cost | What it looks like in practice |
|---|---|
| Abstraction leaks | Provider features that the common interface does not cover are hard to reach, or need provider-specific options. |
| Harder debugging | A wrong answer may come from a prompt the framework built for you. You cannot see it without extra tooling. |
| API churn | Class names, import paths, and parameters change between releases. Old tutorials stop working. |
| Dependency weight | A framework pulls in many packages. More packages mean more install time and more security updates. |
| Learning cost | You must learn the framework's concepts on top of the model's behavior. |
The most important cost is hidden behavior. With the raw SDK, the messages list you write is what the model receives. With a framework, templates, memory, and retrievers may add text before the request leaves your machine.
The framework path has more stops, and each stop is a place where something can go wrong or change without your noticing.
A practical test
Ask three questions before you adopt a framework for a feature.
- Would I otherwise write more than a few hundred lines of plumbing, such as loaders, chunkers, or connectors?
- Do I need to switch providers or vector stores, or support several of them?
- Can I inspect the exact request the framework sends?
If the answers are no, no, and not sure, the raw SDK is usually the better choice. If the first two answers are yes, a framework is likely to pay for itself.
Common errors
- Adopting a framework for one call. The cost is paid in full and the benefit is close to zero.
- Copying code from an old tutorial. Import paths and method names may no longer exist in your installed version.
- Treating the abstraction as a guarantee. Two providers can accept the same framework call and still behave differently, for example on tool calling or JSON output.
- Never looking at the real request. Without logs of the final messages, you are debugging blind.
Exercise
Your app makes one chat call to one provider and validates the reply with a Pydantic model. A teammate proposes adding LangChain "for flexibility." Name two costs you would raise and one condition under which you would agree.
Show answer
Costs: extra dependencies and upgrade work because of API churn, and hidden prompt-building that makes debugging harder. You would agree if the app is expected to grow to several providers or to need prebuilt retrieval components, so the framework would replace plumbing you would otherwise write, and if you can still inspect the exact request sent to the model.