惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
腾讯CDC
D
Docker
The Cloudflare Blog
量子位
爱范儿
爱范儿
L
LangChain Blog
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
Vercel News
Vercel News
MyScale Blog
MyScale Blog

Stories by HARSHA J S on Medium

Mastering LangChain 1.2: Part 13 — Agentic RAG: Why Your AI Needs to Decide When to Google Mastering LangChain 1.2: Part 12 — The Elephant’s Memory: Persistent AI Agents that Never Forget Mastering LangChain 1.2: Part 11 — Custom Middleware Hooks: Orchestrating the AI Lifecycle Mastering LangChain 1.2: Part 10 — Human-in-the-Loop: Adding an “Approval” Button to Your AI Agents Mastering LangChain 1.2: Part 9 — Structured Outputs: Turning Messy Chat into Clean Data Mastering LangChain 1.2: Mastering LangChain 1.2: Part 6 — Dynamic Tool Security: The “Triple-Lock” Guardrail Mastering LangChain 1.2: Part 5 — Dynamic Model Routing: Escalating to Senior AI in Emergencies Mastering LangChain 1.2: Part 4 — Scaling with Middleware and Smart Summarization
Mastering LangChain 1.2:
HARSHA J S · 2026-03-25 · via Stories by HARSHA J S on Medium

Mastering LangChain 1.2: Part 8 — Dynamic System Prompts: Toggling Personas from “Mentor” to “Guardian”

HARSHA J S

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 TypedDict

class 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