Tracing, Middleware, and Key Takeaways

A reliable graph must be visible after it runs. A trace connects the final itinerary to the model calls, tools, retries, checks, and approval that produced it.

One run as a trace tree

The Paris example appears as one parent run with smaller child steps:

flowchart TB P[paris-trip
38.2 s · ₹6.10] P --> R[retrieve_policy
0.4 s] P --> L1[llm_call
2.1 s] P --> T[tool_node › search_flights
4.6 s · 2 retries] P --> C[check_policy
FAIL · §5.1] P --> L2[llm_call · replan
1.9 s] P --> I[interrupt › book_flight
approved 14:02]

Langfuse can record this hierarchy as a trace.

The top level answers:

The child steps answer:

Trace first, then evaluate

A trace is evidence, not a passing score.

Useful checks for this run include:

Check Evidence in the trace
Correct tool use search_flights ran with the intended trip constraints.
Policy handling check_policy recorded FAIL · §5.1, then the graph replanned.
Recovery The flight tool needed two retries and still stayed within its limit.
Human control Booking waited at an interrupt and was approved at 14:02.
Latency and cost Full run: 38.2 seconds and ₹6.10.

Compare these fields across many test trips, not only one successful demo.

Middleware: behaviour around the loop

Middleware adds reusable behaviour without rewriting the agent's main loop.

Examples from the chapter:

flowchart LR I[Input] --> M1[Summarise if needed] M1 --> A[Agent loop] A --> M2[Check call limits] M2 --> M3[Pause risky writes] M3 --> O[Output or next step]

The high-level create_agent API can plug in these behaviours while its underlying LangGraph graph continues to manage execution.

Middleware does not replace business controls. The booking service must still validate permissions and idempotency.

Six things to carry forward

1. LangChain standardises components; LangGraph orchestrates them

create_agent is built on a LangGraph graph underneath.

2. A graph is state, nodes, and edges

Use typed state with reducers, plain Python nodes, and fixed or conditional edges.

3. Workflow patterns are ways of wiring edges

Chaining, routing, fan-out, and loops differ mainly in how control moves. Multi-agent systems are graphs of graphs.

4. Persisted state gives memory, resumption, and approval

Checkpoints per thread make restarts, human pauses, and time travel practical.

5. Middleware adds behaviour without rewriting the loop

Summarisation, approval, and call limits can wrap the agent.

6. Reliability decides whether an agent is deployable

Retry infrastructure failures, surface semantic results, bound loops, and protect write actions with stable keys.

A beginner's build order

  1. Make the goal and success condition explicit.
  2. Declare narrow tools and typed outputs.
  3. Define the smallest useful state.
  4. Give each node one job.
  5. Draw fixed and conditional edges.
  6. Add checkpoints and a thread ID.
  7. Pause before write actions.
  8. Add error-specific retries, timeouts, and idempotency.
  9. Trace the run and evaluate task success, cost, and recovery.

What goes wrong

One-line summary

Use traces to connect outcomes to every step, middleware for reusable controls, and the six core rules to keep LangChain and LangGraph agents understandable and deployable.

Key terms