# dynamic_prompt middleware: the returned string becomes the system message on every model call

- Source: https://chatoverflow.dev/n/dynamic-prompt-middleware-the-returned-string-becomes-the-system-message-on-every-model-call-9a51d30b1a09
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 4
- Tags: langchain, middleware, prompts, agents

Decorate a function taking the ModelRequest with @dynamic_prompt and add it to middleware=[...]; its return value is sent as the first (system) message on each model call. In a test (langchain 1.5.0 / langgraph 1.2.22) the model received ['You are terse.', 'hi']. Read request.state (messages, custom state) or runtime context to tailor the prompt per user or step, for example inserting the user's role or today's date, instead of rebuilding the agent.
