Agentic AI

Agent Frameworks

Why Use a Framework

Module 1 the agent loop, executor, perception

JrCodex·6 min read

Jr Codex Agentic AI Notes

Level: Intermediate Prerequisites: Module 4, Chapter 4: When Planning Fails Time to complete: ~20 minutes


Table of Contents

  1. What You Already Built
  2. What a Framework Actually Provides
  3. What It Costs
  4. The Case for Writing It Yourself
  5. The Honest Decision
  6. Summary & Next Steps

1. What You Already Built

The Inventory
─────────────────────────────────────────
  Module 1   the agent loop, executor, perception
  Module 2   tool schemas, ReAct, system policy
  Module 3   scratchpad, trimming, summarisation,
             long-term stores
  Module 4   plans, dependencies, re-planning,
             reflection, loop and thrash detection

  That is, substantially, an agent framework.
─────────────────────────────────────────
Why This Matters Before Chapter 2
─────────────────────────────────────────
  Frameworks are often taught first, which makes
  them look like magic and makes their failures
  inexplicable.

  Having written the pieces, you can read any
  framework as "their version of the loop, their
  version of memory" — and you can tell when their
  version does not fit your problem.
─────────────────────────────────────────

2. What a Framework Actually Provides

Setting marketing aside, there are five genuine benefits.

1. INTEGRATIONS
─────────────────────────────────────────
  Hundreds of pre-built tools, model wrappers,
  vector store adapters and document loaders.

  This is the largest practical benefit and the
  least intellectually interesting one. Writing
  your own Confluence loader is a week nobody
  should spend.
2. PERSISTENCE AND RESUMPTION
─────────────────────────────────────────
  Checkpointing agent state so a run survives a
  crash, and can be paused for human input and
  resumed.

  Genuinely hard to build well. This is LangGraph's
  strongest argument (Chapter 3) and Module 6,
  Chapter 2's subject.
3. STREAMING AND OBSERVABILITY
─────────────────────────────────────────
  Token streaming through a multi-step run, plus
  hooks that make tracing (Module 7, Chapter 3)
  work without instrumenting every call yourself.
4. CONTROL FLOW PRIMITIVES
─────────────────────────────────────────
  Conditional edges, parallel branches, cycles,
  interrupts. You can write these; a framework has
  already debugged the edge cases.
5. A SHARED VOCABULARY
─────────────────────────────────────────
  "It is a StateGraph with a conditional edge to a
  tool node" communicates instantly to anyone who
  knows the framework. Bespoke architectures have
  to be explained every time, to every new hire.
─────────────────────────────────────────

3. What It Costs

The Four Costs
─────────────────────────────────────────
  ABSTRACTION DISTANCE
    When something goes wrong, the stack trace is
    twelve frames of framework internals. Debugging
    requires learning the framework's model, not
    just your own code.

  VERSION CHURN
    This ecosystem moves fast and breaks interfaces.
    Pin versions, and budget for upgrades that are
    not mechanical.

  HIDDEN PROMPTS
    Many frameworks inject their own prompt text.
    You are debugging a prompt you did not write and
    may not be able to see. Always find out how to
    print the final prompt before committing.

  LEAKY CONTROL
    The moment you need behaviour the abstraction
    did not anticipate, you fight it. Ninety percent
    of the work is faster; the last ten percent can
    be slower than writing it all yourself.
─────────────────────────────────────────
The Hidden Prompt Problem, Concretely
─────────────────────────────────────────
  You set a careful system prompt (Module 2,
  Chapter 2). The framework prepends its own
  scaffolding, appends format instructions, and
  reorders your messages.

  Your agent misbehaves in a way your prompt cannot
  explain — because your prompt is not what the
  model received.

  First thing to learn in ANY framework: how to see
  the actual final payload.
─────────────────────────────────────────

4. The Case for Writing It Yourself

# A complete, production-shaped agent loop. No framework.
def agent(client, goal, tools, schemas, system, bounds):
    messages = [{"role": "system", "content": system},
                {"role": "user", "content": goal}]
 
    while True:
        allowed, reason = bounds.before_step()
        if not allowed:
            return partial_result(messages, reason)          # Module 4, Chapter 4
 
        reply = client.chat.completions.create(
            model="gpt-4o", messages=messages, tools=schemas).choices[0].message
        bounds.record(cost_of(reply))
 
        if not reply.tool_calls:
            return reply.content
 
        messages.append(reply)
        for call in reply.tool_calls:
            if nudge := bounds.check_loop(call):
                messages.append({"role": "tool", "tool_call_id": call.id,
                                 "content": nudge})
                continue
            result = safe_execute(tools, call)               # errors as observations
            messages.append({"role": "tool", "tool_call_id": call.id,
                             "content": compress(result)})
What This Buys
─────────────────────────────────────────
  You can read the whole thing. Every prompt is
  yours. Every stack frame is yours. Adding a
  behaviour means writing it, not discovering
  whether the framework permits it.

  For a single agent with a handful of tools, this
  is roughly 150 lines including memory and bounds,
  and it will outlive three framework major
  versions.
─────────────────────────────────────────
When Bespoke Stops Scaling
─────────────────────────────────────────
  - You need durable state across restarts
    (Module 6, Chapter 2). This is genuinely hard.
  - You need many integrations you would otherwise
    write and maintain.
  - Several agents must coordinate with shared
    state.
  - A team needs a common vocabulary to work in.

  Any two of those, and a framework is probably
  correct.
─────────────────────────────────────────

5. The Honest Decision

Decision Guide
─────────────────────────────────────────
  One agent, <10 tools, one codebase
      ──► WRITE IT. The loop is 150 lines and you
          will understand every failure.

  Need durable state, pause/resume, human approval
  mid-run
      ──► LangGraph (Chapter 3). This is the
          strongest case for a framework.

  Need many pre-built integrations
      ──► LangChain components (Chapter 2) — often
          worth using for the tools and loaders even
          if you keep your own loop.

  Multiple specialised agents conversing
      ──► AutoGen or CrewAI (Chapter 4), and read
          Module 6, Chapter 5 first.

  Prototyping to find out whether the idea works
      ──► whichever you can move fastest in. Expect
          to rewrite it.
─────────────────────────────────────────
The Pattern Worth Recommending
─────────────────────────────────────────
  Many mature teams land in the same place:

    OWN the loop, the prompts and the state.
    BORROW the integrations, the checkpointer and
    the tracing.

  Frameworks are more valuable as component
  libraries than as control flow. Take the document
  loaders and the vector store adapters; keep the
  ten lines that decide what your agent does next.
─────────────────────────────────────────

6. Summary & Next Steps

Key Takeaways

  • Modules 1–4 built substantially what a framework provides, which is what makes frameworks readable rather than magical.
  • The genuine benefits are integrations, durable persistence and resumption, streaming and tracing hooks, control-flow primitives, and a shared team vocabulary.
  • The real costs are abstraction distance when debugging, version churn, hidden injected prompts, and fighting the abstraction on the last ten percent.
  • A single agent with a few tools is ~150 lines of your own code; frameworks earn their place at durable state, many integrations, or multi-agent coordination.

Concept Check

  1. What is the first thing to learn about any agent framework before committing to it, and why?
  2. Which framework benefit is genuinely hard to build yourself, and which module covers the problem it solves?
  3. Describe the "own the loop, borrow the components" pattern and why teams converge on it.

Next Chapter

Chapter 2: LangChain


Jr Codex — 1-on-1 Personalized Coaching | Back to Module Index | Back to Agentic AI Index