Chapter 13 · Frameworks

Don't build it all
from scratch.

Frameworks hand you the plumbing — LangChain, LlamaIndex, AutoGen, CrewAI — then the advanced patterns give you the edge.

The case for frameworks

They give you the plumbing.

You could hand-write the agent loop, the tool routing, the memory, the retries, the JSON parsing — for every single agent, forever. That's a special kind of insane.

Frameworks hand you all of that so you spend your time on what your agent actually does. You write the job; they run the mechanics.

Under the sink

What you stop rewriting.

The loop

Perceive, think, act, observe — the cycle, wired up and turning for you.

Tool routing

Registering tools, matching a call to a function, running it, feeding the result back.

Memory & retries

State across turns, plus graceful recovery when a call fails or returns junk.

Parsing

Turning the model's messy text into clean, structured, typed data you can use.

Integrations

Pre-built connectors to models, databases, and dozens of external services.

Orchestration

Multiple agents handing work to each other without you writing the choreography.

The toolbox

Four frameworks, four jobs.

Think of these like power tools. A hand saw cuts wood, but a table saw cuts it faster and straighter — as long as you pick the right one for the cut.

Each framework is tuned for a different job. Here's when to reach for each.

Pick the right one

The four frameworks.

LangChain / LangGraph Most popular

The default starting point — 100+ integrations for models, tools, and databases. LangGraph adds stateful, multi-agent workflows with loops and branching. Use for general-purpose production pipelines.

LlamaIndex Best for RAG

Built to connect an LLM to your documents — best-in-class retrieval, strong document processing, and 50+ data connectors. Use for knowledge bases and Q&A over your own data.

AutoGen From Microsoft

Pioneered multi-agent conversation — agents that message each other to solve a problem, with built-in code execution. Use for autonomous coding and research where agents debate.

CrewAI Beginner-friendly

The gentlest on-ramp to multi-agent systems. Define each agent by its role, hand it a goal and tools, and let the crew collaborate. Use for role-based teams, up fast.

In real code

A crew, in a dozen lines.


from crewai import Agent, Task, Crew

# define agents by role — you write job descriptions, not control flow
researcher = Agent(
    role="Researcher",
    goal="Find the 3 strongest sources on the topic",
    tools=[web_search],
)
writer = Agent(
    role="Writer",
    goal="Turn the research into a tight one-page brief",
)

# one task, handed to the crew
brief = Task(description="Research and write the brief", agent=writer)

crew   = Crew(agents=[researcher, writer], tasks=[brief])
result = crew.kickoff()          # they hand work to each other
print(result)

No loop, no message plumbing, no tool router — you described a team and the framework ran it.

The differentiators

The patterns tutorials skip.

Frameworks get you running. These four techniques are what separate a flashy demo from a system serious practitioners actually trust in production.

They're how you keep agents cheap, reliable, and self-improving.

The edge

Four advanced patterns.

🔁 Self-modifying agents

The agent reads a small rules file each session and rewrites it when you correct a mistake — so it never repeats the same error twice. Reliable & self-improving.

🎲 Stochastic consensus

Spawn ~10 agents on one prompt; temperature makes each answer differ. Where they agree is your reliable answer; where they diverge is idea fuel. Robust.

🧊 The iceberg technique

Keep only core rules visible; give the agent grep and read tools to pull in exactly what it needs. Cuts token cost 60–80%. Cheap.

💸 The 60-30-10 cost rule

Route 60% of tasks to cheap models, 30% to mid-tier, 10% to your top model. Same quality where it matters, a fraction of the bill. Cheap.

Pattern 1 · in depth

Agents that rewrite their own rules.

The agent opens every session by reading a small rules-and-memory file. When you correct a mistake, it appends the lesson to that file.

Next session it opens with the corrected rules and doesn't repeat the error. Over time it stops needing the same feedback twice — it improves itself.

Pattern 2 · in depth

Ten rolls of the dice, then compare.

Run ~10 agents on the exact same prompt. Because temperature adds randomness, each returns a slightly different take.

Where they agree

You've found the reliable answer — one bad roll can't dominate the vote.

Where they diverge

You've surfaced the creative edge cases — the interesting outliers become idea fuel.

Pattern 3 · in depth

The iceberg technique.

Don't dump your whole codebase or knowledge base into context — most of it is dead weight the model pays for on every call.

Keep only the core rules visible above the waterline. Give the agent grep and read tools to pull in exactly the piece it needs, when it needs it. Cuts token cost 60–80%.

Pattern 4 · in depth

Route tasks by difficulty: 60-30-10.

100 tasks sorted by difficulty 60% · Cheap, fast model simple — classify, format, extract 30% · Mid-tier model moderate — research, synthesis 10% · Top model complex — orchestrate, high-stakes

Not every task deserves your smartest, priciest model. Same quality where it matters — a fraction of the bill.

The real lesson

Plumbing below, patterns above.

Frameworks give you the plumbing. The advanced patterns are how serious practitioners keep agents cheap, reliable, and self-improving.

That's the difference between a flashy demo and something you'd actually ship.

Chapter 13 in one breath

Use the plumbing. Master the patterns.

LangChain for pipelines, LlamaIndex for RAG, AutoGen for agent conversation, CrewAI for role-based teams — pick the tool that fits the cut. Then layer on what tutorials skip: agents that rewrite their own rules, consensus from many parallel runs, the iceberg for context, and 60-30-10 for cost.

Next up

Chapter 14 · Safety & Guardrails.

We can build agents fast and run them cheap. Now the question that decides whether you can deploy them at all — how do we keep them safe?

Press S for speaker notes · F for fullscreen · to advance.