Prompt optimization
Prompt optimization is LangMem's procedural memory: create_prompt_optimizer reads conversations and feedback on them and returns an improved system prompt, so the agent's behaviour changes, not only what it knows.
Last updated: 30 Sep, 2026 · LangMem 0.0.30
Semantic and episodic memories are facts and examples the agent reads. Procedural memory is different: it is the agent's own instructions. When a reviewer says "it answered in the chat after the customer asked for email", the fix is a rule in the system prompt, and a prompt optimizer writes that rule.
create_prompt_optimizer, run on this page with Groq.Instructions that learn from outcomes
The clip's points: procedural memory lives in the system prompt, while semantic memories are injected as user facts and episodes as context; the model reads procedures as directives, not background; and procedures are learned from outcomes rather than stated once. That makes it a bridge to agents that improve themselves, updating their own instructions as they work.
Syntax:
optimizer = create_prompt_optimizer(model, kind="prompt_memory") # or "metaprompt", "gradient"
new_prompt = optimizer.invoke({"prompt": old_prompt, "trajectories": [(conversation, feedback)]})A conversation that went wrong, and the feedback
A trajectory is a conversation paired with feedback: a string, a dictionary like {"score": 0}, or None to let the optimizer judge the conversation itself.
conversation = [
{"role": "user", "content": "Please answer me by email, not here."},
{"role": "assistant", "content": "Here is your answer in the chat: your refund was sent on Monday."},
]
feedback = "The customer asked for email. Always reply on the channel the customer asks for."An optimizer
kind="prompt_memory" makes one model call: the current prompt, the trajectories and their feedback go in, the full updated prompt comes out.
optimizer = create_prompt_optimizer(model, kind="prompt_memory")Improving the support prompt
from langchain.chat_models import init_chat_model
from langmem import create_prompt_optimizer
model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0)
conversation = [
{"role": "user", "content": "Please answer me by email, not here."},
{"role": "assistant", "content": "Here is your answer in the chat: your refund was sent on Monday."},
]
feedback = "The customer asked for email. Always reply on the channel the customer asks for."
old_prompt = "You answer support tickets for an online shop."
optimizer = create_prompt_optimizer(model, kind="prompt_memory")
new_prompt = optimizer.invoke({"prompt": old_prompt, "trajectories": [(conversation, feedback)]})
print("BEFORE:", old_prompt)
print("AFTER: ", new_prompt)BEFORE: You answer support tickets for an online shop. AFTER: You answer support tickets for an online shop. When a customer requests a specific communication channel (such as email), respond using that channel and do not provide the answer in the chat.
What changed in the prompt
- The original sentence is kept and a rule is added after it, taken from the feedback: reply on the channel the customer asks for.
- The rule is written as an instruction, "respond using that channel and do not provide the answer in the chat", which is how procedural memory differs from a stored fact.
- Nothing checked it. Run the new prompt on saved conversations before the agent uses it.
The three optimizer kinds
| kind | Model calls | How it works |
|---|---|---|
| prompt_memory | One | Prompt, trajectories and feedback in; the updated prompt out. |
| metaprompt | One or more | Reflects on the trajectories, for up to max_reflection_steps rounds, before writing the prompt. |
| gradient | Two or more | One call writes a critique of what went wrong, another applies it to the prompt. |
When to optimize a prompt
- After collecting reviewer feedback on real conversations.
- When a rule keeps being broken and you want it written into the instructions, not remembered per user.
- For multi-agent systems,
create_multi_prompt_optimizerspreads the changes across each agent's prompt.
Related
- Previous: Running summaries
- Next: Integrations
- Reference: How to optimize a prompt
- Add a second trajectory with the feedback "Too long. Keep replies under three sentences."
- Pass
Noneas the feedback and see what the optimizer infers from the conversation alone. - Change
kindto"metaprompt"withconfig={"max_reflection_steps": 1}and compare.
Little by little, you're building something great.