ai/from-scratch

Stage 9 of 12 · about 3.2 h

Agentic AI patterns

Route, chain, plan, reflect, remember, and delegate, each with a budget and a gate.

An agent loop is one pattern among several, and often not the best one. You start with workflows: a voting router and a chain of narrow steps with code gates, so Pocket's common questions run the same way every time at a known cost. Then a planner emits a JSON plan that code validates, workers run the steps with fresh contexts, a failed step triggers one bounded replan, and a critique loop revises the answer against a rubric until it passes or stops improving. You give Pocket memory that survives sessions, written through a policy with sources and dates, and compaction that keeps long conversations under a token budget without breaking the tool protocol. Last, a lead agent dispatches scoped subagents in parallel, merges their reports, and you compute what that costs against one agent. Every lab runs offline on a scripted stand-in model.

Before you start
You can run a bounded agent loop with tool_use and tool_result blocks, guard its tools, force structured output, and ground answers in retrieved notes, as in stages 4 to 7.
When you finish
pocket/stage9/ holds workflows.py, plan_reflect.py, memory.py and multi_agent.py: a gated router and chain, a planner with workers, replanning, and a capped critique loop, a memory store with compaction, and a lead with scoped subagents whose token cost you have measured.
Already know this? Skip it, or take the placement check. Skipped lessons stay open.
  1. Lesson 1 · 45 min

    Workflows before agents

    You can place a task on the spectrum from one call to a workflow to an agent, build a router and a prompt chain with code gates between the steps, use voting where one call is not reliable enough, and say when a fixed workflow beats an agent.

  2. Lesson 2 · 50 min

    Planning, replanning, and reflection

    You can have a planner emit a JSON plan, validate it in code, run each step in a worker with a fresh context, replan within a budget when a step fails, and improve a draft with a bounded generate, critique, revise loop that stops when the rubric passes or stops improving.

  3. Lesson 3 · 45 min

    Memory and context

    You can treat the context window as working memory, keep a long-term memory store with a write policy (provenance, dates, recency wins, no secrets), recall memories into a prompt, and compact a conversation at a token budget without breaking a tool_use and its result.

  4. Lesson 4 · 50 min

    Multi-agent systems

    You can build a lead agent that dispatches subagents with isolated contexts, scoped tools, and clear briefs, runs them in parallel, merges their reports with a single writer, and compute what the design costs in tokens against a single agent.