Skip to main content
Time to get hands-on. In this lesson you’ll extend a minimal ReAct-style agent with three practical production patterns: structured output, input guardrails, and a human-in-the-loop confirmation step. These are additive patterns — you do not need to rewrite the agent core; you augment it. This lab assumes a ready environment:
  • Python 3.11 and a virtual environment
  • The OpenAI Python SDK installed
  • Your working directory: /root/code
Create a file named patterns_agent.py. Start from the minimal ReAct structure: import modules, create an OpenAI client, set a system prompt that describes the Thought / Action / Observation markers, add a check_calendar tool and handler, and wire up the loop that runs until the agent produces a final answer. Minimal starting template (ReAct structure):
Next, add the three patterns one-by-one and then run the agent. Below is a consolidated, runnable example that demonstrates all three patterns together: structured output, input guardrails, and a human-in-the-loop confirmation for high-stakes actions such as sending email. The script is intentionally simple and synchronous for clarity — its goal is to illustrate how these patterns integrate into a ReAct-style loop.
The check_input guard runs locally before any API call, so disallowed queries never reach the model and cost no tokens.
Final consolidated example (patterns_agent.py):
How the script demonstrates each pattern
  • Structured output
    • The system prompt instructs the model to emit actions in a single-line JSON Action form and to conclude with a JSON summary block containing summary and actions_taken. This makes downstream parsing deterministic and machine-readable.
  • Input guardrails
    • check_input runs entirely locally before any API call. If it detects blocked terms (for example: medical, legal, or financial advice), the script rejects the user request and never calls the model — saving tokens and preventing the model from handling sensitive requests.
  • Human-in-the-loop
    • When the agent emits an action whose name is send_email, the driver pauses and prompts a human operator to confirm. If the user denies, the tool returns an “Email cancelled by user.” observation and the agent re-plans. Lower-risk tools such as check_calendar run automatically.
Summary table — patterns at a glance: Run the full stack
  1. Ensure the environment variables OPENAI_API_KEY (and OPENAI_API_BASE if using a non-default base) are set.
  2. Run:
    • python patterns_agent.py
  3. Example behavior with user_message = "Email Sarah my calendar summary for today.":
    • The guardrail runs and passes.
    • The agent may call check_calendar automatically.
    • When the agent requests send_email, the script prompts: Send this email? (y/n):
      • Type y to simulate sending (the send_email implementation is a stub).
    • The agent finishes and prints a final machine-readable JSON summary.
Best practices and next steps
  • Replace the stubbed check_calendar and send_email with real integrations and robust error handling in production.
  • Consider adding a loop counter or timeout to protect against infinite planning loops.
  • Expand check_input to use more advanced safety checks (regular expressions, allowlists, or a dedicated moderation service).
  • Log actions_taken and Observations to a secure audit trail for observability and compliance.
Links and references
  • OpenAI Python SDK docs
  • ReAct and agent design patterns: search for ReAct agent papers and blog posts for design inspiration.
This pattern preserves the ReAct flow while adding small, composable production controls that improve safety, observability, and human oversight without rewriting the agent logic.

Watch Video