Choose a framework from the control your application needs
Agent frameworks provide reusable primitives for state, tools, routing, memory, multi-agent coordination, checkpoints, tracing, and deployment. They reduce plumbing but do not replace system design.
What You Will Learn
- Identify the concrete runtime need a framework should solve.
- Compare plain code, LangGraph, CrewAI, and AutoGen by abstraction and control style.
- Relate state graphs, crews/flows, and AgentChat/Core to suitable use cases.
- Keep business tools portable behind framework-neutral interfaces.
Start from the control problem, not the framework name
Plain code is often clearest for a small loop because every state update, permission check, and stop rule is visible. A framework becomes useful when the system needs durable checkpoints, explicit graphs, human interrupts, reusable team orchestration, event-driven messaging, or integrated tracing that would otherwise become substantial plumbing.
LangGraph centers explicit state and graph transitions, with persistence and interrupt/resume patterns. CrewAI centers agents and crews for role-oriented collaboration and flows for structured orchestration. AutoGen provides higher-level AgentChat abstractions and a lower-level Core for flexible event-driven multi-agent applications. These descriptions guide selection without claiming one framework is universally best.
Instructional figure
Choose an agent framework from the runtime need
Which concrete requirement justifies adding a framework?
Framework concepts and trade-offs
Framework selection checklist
Protect domain logic from framework lock-in
Define your search, billing, validation, and other business capabilities behind interfaces owned by the application. Framework-specific nodes or agents call those interfaces. This keeps authorization and tests stable and lets the orchestration layer change without rewriting every business tool.
Apply four control models to the same research brief
Suppose the product must gather three approved sources, extract price and warranty, obtain a reviewer decision when sources conflict, and save a trace. The tools, permission checks, evidence schema, and acceptance test should stay framework-neutral; only the orchestration model changes.
Plain code makes the loop and state updates explicit. LangGraph represents the steps and conditional transitions as a state graph. CrewAI can represent researcher and reviewer roles coordinated toward one deliverable. AutoGen can represent the same specialists as conversational or event-driven agents with an explicit termination rule. A framework is justified only when its control model solves measured complexity better than the dependency it adds.
Compare frameworks against the same task
Prototype the agent loop with plain code first.
List required features such as durable state or multi-agent roles.
Compare framework control, debugging, ecosystem, persistence, and lock-in.
Wrap framework-specific code behind your own interfaces and evaluate behavior.
Instructional figure
One research-brief problem, four control models
What changes when the task is expressed in plain code, LangGraph, CrewAI, or AutoGen?
Plain code
One explicit state object → functions → loop → developer-owned checkpoint and logs
LangGraph
Typed state → named graph nodes/edges → persistence + interrupt/resume
CrewAI
Researcher + reviewer roles → crew/flow coordination → shared deliverable
AutoGen
Conversational/event-driven agents → messages → termination condition
Same problem, different control ownership
Worked Example: Choose a framework by mapping one bounded workflow
The same research brief needs three approved sources, price and warranty extraction, conflict review, a durable trace, and a deterministic verifier. Business tools and schemas remain identical in all four designs.
- Plain code is the reference: one ResearchState object moves through explicit functions and a bounded loop. It is easiest to audit, but the team must build persistence, interrupts, and tracing it truly needs.
- LangGraph makes search, extraction, conflict review, and END explicit nodes and transitions. It is a strong conceptual fit when resumable state and human interrupts are core requirements.
- CrewAI expresses a researcher and reviewer as role-oriented collaborators, while a flow coordinates the deliverable. It fits when measured role specialization matters more than a minimal loop.
- AutoGen represents researcher/reviewer coordination through agent messages or events and requires a clear termination condition. It fits when that interaction model is itself useful rather than merely fashionable.
What the result means: No framework changes the authorization or correctness contract. Select the smallest control model that makes state, intervention, and traces clearer, and keep domain tools portable so a later migration does not rewrite business logic.
A Realistic Example: Choosing among common patterns
Use a graph-style orchestrator for explicit stateful branches, role-oriented orchestration for collaborative prototypes, and plain functions for a small deterministic workflow.
Read the Mechanism in Code
Use the label and note to understand what this block represents before reading each line.
class AgentRuntime(Protocol):
def run(self, state: TaskState) -> TaskResult: ...
# Keep business tools independent of LangGraph, CrewAI, AutoGen,
# or another orchestration framework.Hands-On Runnable Python Lab: Apply a Release Contract to Two Candidate Adapters
Two fictional adapter result sets are compared—not LangGraph, CrewAI or AutoGen benchmarks. This standard-library evaluator requires correct outcomes and no unauthorized or duplicate writes.
- Define success, missing-evidence, denied-action and retry contracts.
- Check candidate A’s recorded results.
- Check candidate B, which writes on a denied action and duplicates a retry.
- Report each failed contract, not merely an average hiding safety failures.
Correctness gates come before choosing an orchestration library
A and B are fictional application adapters. The same contract tests can evaluate plain code or a framework implementation; this is not a product benchmark.
expected = {
"supported": ("success", 1),
"no_evidence": ("abstain", 0),
"denied": ("denied", 0),
"retry": ("success", 1),
}
candidates = {
"adapter A": {"supported": ("success", 1), "no_evidence": ("abstain", 0),
"denied": ("denied", 0), "retry": ("success", 1)},
"adapter B": {"supported": ("success", 1), "no_evidence": ("abstain", 0),
"denied": ("success", 1), "retry": ("success", 2)},
}
for name, outcomes in candidates.items():
failures = [case for case, contract in expected.items() if outcomes[case] != contract]
print(f"{name}: {len(expected) - len(failures)}/{len(expected)} contracts passed")
print(" failed cases:", ", ".join(failures) or "none")
print(" eligible:", not failures)
adapter A: 4/4 contracts passed
failed cases: none
eligible: True
adapter B: 2/4 contracts passed
failed cases: denied, retry
eligible: FalseWhat This Run Shows
- B passes the ordinary success test but fails denial and retry behavior.
- Write counts are observable contract fields, not inferred from response text.
- Replace fixtures with actual captured adapter outcomes to evaluate named frameworks.
After correctness gates pass, compare developer effort, trace quality, latency, resource use and operational fit. Popularity cannot substitute for these measurements.
From the Exercise to an Application
Experiment: Make adapter B return denied but leave that case’s write count at 1.
It still fails. A refusal message after an unauthorized write is not safe behavior.
Summary / Key Takeaways
- Agent frameworks provide reusable orchestration/runtime capabilities, but they do not replace good task design, tools, security, or evaluation.
- Plain code is often best for small deterministic or lightly agentic systems because it minimizes abstraction and lock-in.
- LangGraph is especially useful when explicit state, graph control, persistence, and human-interrupt/resume behavior are central requirements.
- CrewAI emphasizes agents, crews, and flows for role-oriented collaboration plus structured workflow control.
- AutoGen provides AgentChat and Core abstractions for conversational and event-driven single/multi-agent applications.
- Framework choice should be based on the control model, durability, coordination, observability, ecosystem, and deployment needs—not popularity.
- Keep business tools and domain logic behind your own interfaces where possible so framework changes do not rewrite the whole application.
Verified Further Reading
These primary research papers and current official specifications support the concepts and implementation guidance in this lesson. The explanations, analogies, exercises, and code above remain original teaching material.
LangGraph: Overview
Current framework documentation for graph-based stateful workflows, persistence, interrupts, memory, and human-in-the-loop control.
CrewAI: Introduction
Current framework documentation describing agents, crews, flows, state, and execution control.
Microsoft AutoGen: AgentChat
Official documentation for single-agent and multi-agent applications, teams, handoffs, and human feedback.