LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Llms
  • Profile
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangChain
UniversalHubNodeLoadSerializableEncoder BackedFile SystemIn Memory
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsLlmsProfileLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingHashJson PatchJson SchemaMathSsrfStreamTestingTiktokenTypesVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScriptlangchainindexdynamicSystemPromptMiddleware
Functionā—Since v1.1

dynamicSystemPromptMiddleware

Dynamic System Prompt Middleware

Allows setting the system prompt dynamically right before each model invocation. Useful when the prompt depends on the current agent state or per-invocation context.

Copy
dynamicSystemPromptMiddleware<
  TContextSchema = unknown
>(
  fn: DynamicSystemPromptMiddlewareConfig<TContextSchema>
): AgentMiddleware<undefined, undefined, unknown, readonly ClientTool | ServerTool[]>

Used in Docs

  • Agents
  • Build a RAG agent with LangChain
  • Short-term memory
  • LangChain v1 migration guide

Parameters

NameTypeDescription
fn*DynamicSystemPromptMiddlewareConfig<TContextSchema>

Function that receives the current agent state and runtime, and returns the system prompt for the next model call as a string.

Example

Copy
import { z } from "zod";
import { dynamicSystemPrompt } from "langchain";
import { createAgent, SystemMessage } from "langchain";

const contextSchema = z.object({ region: z.string().optional() });

const middleware = dynamicSystemPrompt<z.infer<typeof contextSchema>>(
  (_state, runtime) => `You are a helpful assistant. Region: ${runtime.context.region ?? "n/a"}`
);

const agent = createAgent({
  model: "anthropic:claude-sonnet-4-5",
  contextSchema,
  middleware: [middleware],
});

await agent.invoke({ messages }, { context: { region: "EU" } });
View source on GitHub