Overview

14 Productionizing AI agents: Memory, guardrails, and beyond

Productionizing AI agents requires more than connecting a language model to tools. This chapter explains how to make agents reliable, safe, and resilient in real-world settings by adding memory and guardrails, using a travel information assistant as a running example. It shows how to preserve conversational context across turns, enforce scope and safety policies at multiple stages of a workflow, and design for robustness when things go wrong—ultimately equipping teams to deliver assistants that maintain user trust through consistent behavior.

The chapter first adds short-term memory via LangGraph checkpoints, which snapshot the full graph state after each node so the system can resume with context, recover from failures, and support human-in-the-loop pauses. Sessions are identified by a thread_id, and a checkpointer (in-memory for development or persistent backends like SQLite/PostgreSQL for production) stores snapshots. With OpenAI’s Responses API configured to reuse the previous response ID, the assistant avoids resending full histories while still resolving follow-ups such as “same town.” The text also demonstrates inspecting state history and “rewinding” to a prior checkpoint, illustrating how checkpointing underpins multi-turn continuity beyond simply storing message logs.

Guardrails are then layered in to keep the agent within scope and policy. The chapter outlines rule-, retrieval-, and model-based approaches and shows where to enforce them: pre-model, routing, tool-level, and post-model. Concretely, a lightweight classifier at the router blocks non-travel queries and routes them to a refusal path, while agent-level pre_model hooks impose Cornwall-specific restrictions for both travel information and accommodation booking. It closes with “beyond” guidance—post-model checks for safety and tone, human-in-the-loop escalation for ambiguous or high-impact tasks, long-term memory design and privacy, systematic evaluation (functional, behavioral, performance, regression), and deployment options on LangGraph Platform or the Open Agent Platform—so teams can move from prototype to scalable, compliant, and cost-efficient production systems.

Sequence diagram of the travel assistant with checkpoints saved after each node execution, allowing state restoration across turns using a shared thread_id.
Updated travel assistant workflow: A guardrail checks if the user’s question is in scope. Irrelevant queries are routed to guardrail_refusal, which sends them directly to END, bypassing all other agents.
Flowchart of updated router logic with guardrail check: queries first pass a relevance filter, and irrelevant ones are sent with a refusal message directly to the guardrail_refusal node.

Summary

  • Memory enables natural conversation — Short-term conversational memory preserves context across turns, allowing the assistant to handle follow-up questions without repeating information.
  • Checkpoints store full execution state — LangGraph saves snapshots after each node, enabling both conversation continuity and recovery from failures.
  • Guardrails maintain scope and safety — They filter or redirect requests that fall outside the assistant’s domain or policy guidelines.
  • Multiple guardrail layers are valuable — Use router-level checks for fast rejection and agent-level checks for specific data or domain constraints.
  • Post-model guardrails act as a final safety net — They verify output correctness, tone, and format before the user sees it or a downstream system processes it.
  • Human-in-the-loop improves reliability — Escalating certain cases to human review can prevent costly mistakes and provide training data for future automation.
  • Evaluation is ongoing, not one-off — Regular functional, behavioral, performance, and regression testing ensures the assistant remains trustworthy over time.
  • Production readiness is holistic — Combining memory, guardrails, human review, and evaluation sets the foundation for safe, effective, and scalable AI agents.

FAQ

What is short-term memory in LangGraph and why is it needed?Short-term memory retains context within a single user session so the agent can understand follow-ups like “same town” without restarting the conversation. In LangGraph, this is implemented via checkpoints that store conversation and execution state at each node, enabling natural, stateful interactions.
How do LangGraph checkpoints work and what is a super-step?A checkpoint is a snapshot of the graph’s state taken after a super-step—the execution of a single node (parallel nodes are grouped). By saving after each node, the system can resume, replay, or provide full conversation history to the LLM on subsequent turns.
How do I add conversational memory to the travel assistant?Generate a thread ID for the session and pass it in the config on every invoke; compile the graph with a checkpointer; then call the graph with both state and config each turn. The thread ID ties all checkpoints to one session and the checkpointer persists state across turns.
Which checkpointer storage should I use in development vs production?Use InMemorySaver for local dev and tests. For production, use a persistent backend such as SqliteSaver or, preferably, PostgresSaver (or their async variants) for durability, scalability, and concurrency support.
How do I configure the OpenAI Responses API for conversation continuity?Enable use_responses_api and use_previous_response_id on ChatOpenAI. With use_previous_response_id, only the prior response ID is sent and OpenAI rehydrates history; if omitted, sending the full history triggers a duplicate-submission error.
How can I rewind to a past checkpoint and resume execution?Fetch state history with get_state_history(config), extract the latest checkpoint_id and thread_id, build a new config containing both, optionally inspect with get_state(new_config), “rewind” by invoking(None, new_config), then send the follow-up question with the same new_config.
What are guardrails and where can they be applied in the workflow?Guardrails keep the agent in scope and safe. Common types: rule-based, retrieval-based, and model-based. Insert them as pre-model checks (validate inputs), routing-stage checks, tool-level checks, and post-model checks (validate outputs before user delivery).
How do I implement a router-level guardrail to reject non-travel questions?Use a lightweight LLM classifier (e.g., GuardrailDecision) with a strict system prompt to decide if a query is travel-related. If not, inject a refusal message and route to a guardrail_refusal node that goes directly to END, bypassing downstream agents.
How do I enforce Cornwall-only scope at the agent level?Create a pre_model_guardrail function that classifies the latest user message against a Cornwall-specific policy. If out of scope, prepend a refusal instruction to llm_input_messages; otherwise, pass through unchanged. Attach this via pre_model_hook to both the travel info and accommodation booking agents.
What else should I consider for production beyond memory and guardrails?Plan for long-term user/app memory (with privacy and summarization), human-in-the-loop escalation, post-model guardrails (fact filters, redaction, tone, schema checks), systematic evaluation (functional, behavioral, performance, regression), and deployment on LangGraph Platform or Open Agent Platform (managed SaaS or private cloud).

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • AI Agents and Applications ebook for free