Mastering LangChain 1.2: Part 8 — Dynamic System Prompts: Toggling Personas from “Mentor” to “Guardian”
In Part 8 of our series,today we are focusing on Dynamic System Prompts. This is a powerful technique for ensuring operational safety and tailoring user experience.
In most AI tutorials, the System Prompt is a static string defined at the start of the code. Something like: “You are a helpful assistant.”
But in professional environments, a single persona is rarely enough. An AI agent should not treat a Production cluster with the same casual, educational tone it uses in a Development sandbox. In Dev, you want a “Mentor” who explains mistakes and encourages exploration. In Prod, you want a “Guardian” who is concise, cautious, and strictly enforces safety protocols.
Today, we’re using LangChain 1.2’s @dynamic_prompt middleware to build an environment-aware SRE agent.
The Problem: Persona Mismatch
Imagine an agent with a
delete_pod tool.
- In Dev: If a junior developer asks to delete a pod, the agent should say: “Sure! Deleting the pod now. By the way, here is why that pod was failing…”
- In Prod: If that same developer asks to delete a pod, the agent should say: “Refused. Manual deletions in Production are prohibited without an approved ticket. Deletion aborted.”
To achieve this without writing complex if/else logic inside every single tool, we use Dynamic System Prompts.
1. Defining the Environment Context
First, we define what our environment looks like. We’ll use a simple TypedDict to track the environment name and the user’s role.
from typing import TypedDictclass EnvContext(TypedDict):
env_name: str # 'dev' or 'prod'
user_role: str # 'junior' or 'senior'
2. The Dynamic Persona Middleware
Using the @dynamic_prompt decorator, we can write a function that generates a completely different set of instructions based on our runtime metadata.
from langchain.agents.middleware import dynamic_prompt@dynamic_prompt
def environment_aware_prompt(request: ModelRequest) -> str:
ctx = request.runtime.context
env = ctx.get("env_name", "dev")
# BASE PROMPT
base = "You are a DevOps Assistant."
# PRODUCTION (The Guardian)
if env == "prod":
return f"{base} 🚨 CURRENT ENVIRONMENT: PRODUCTION 🚨. Be concise. Treat every command as dangerous. Refuse deletions immediately."
# DEVELOPMENT (The Mentor)
else:
return f"{base} 🌱 Current Environment: Development. Be helpful and educational. Explain your commands. Safe space for experimentation."
3. One Agent, Two Lives
By plugging this middleware into our create_agent call, our bot effectively gains a "split personality."
agent = create_agent(
model=model,
tools=[delete_pod],
middleware=[environment_aware_prompt], # <--- Plug it in
context_schema=EnvContext
)Now, let’s see how it handles the exact same request: “Delete the database pod please.”
Scenario 1: Testing in Dev
- Result: “Sure! I’ve deleted the pod for you. In a development environment, this is a safe way to trigger a fresh deployment…”
Scenario 2: Escalating to Prod
- Result: “REFUSED. Unauthorized deletion attempt in Production. Only ‘CONFIRM DELETION’ commands are accepted after manual review. Operation canceled.”
Conclusion: Selective Boundaries
The power of dynamic system prompts lies in Operational Safety. Instead of relying on the LLM to “remember” it is in production via a long, bloated prompt, we feed it a concise, environment-specific instruction for every single turn.
By combining this with Dynamic Model Routing (Part 5) and Dynamic Tool Security (Part 6), you can build an AI agent that is perfectly specialized for any context it finds itself in.
💬 What do you think?
Drop your thoughts, questions, or suggestions in the comments below!
Check out my YouTube channel for more exciting content! [YouTube Channel Link — Harsha Selvi]
Disclaimer: This text has been rephrased using AI tools, and some parts are derived from various sources to provide a comprehensive overview.
#AI #LangChain #MachineLearning #DevOps #SRE #Python #AIEngineering #Automation











