LangMemLangMem 0.0.30 · LangGraph 1.2 · Python 3.10+
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
19 small wins to finish your pathNext lesson →

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.

Procedural memory · from the AI Security Course · 320:10 to 324:03
The video's notebook describes procedural memory as a library of learned procedures, with its FinCoach finance assistant as the example. LangMem keeps procedural memory in the system prompt itself and updates it with 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:

python
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.

python
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.

python
optimizer = create_prompt_optimizer(model, kind="prompt_memory")

Improving the support prompt

ExampleAPI key
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)

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

kindModel callsHow it works
prompt_memoryOnePrompt, trajectories and feedback in; the updated prompt out.
metapromptOne or moreReflects on the trajectories, for up to max_reflection_steps rounds, before writing the prompt.
gradientTwo or moreOne 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_optimizer spreads the changes across each agent's prompt.
Watch out. An optimized prompt is a new version of your agent. As the video says right after this clip, a bad procedure that gets reinforced causes systematic errors. Keep prompts in version control, review each change, and test it on saved conversations before switching.
Try it yourself
  • Add a second trajectory with the feedback "Too long. Keep replies under three sentences."
  • Pass None as the feedback and see what the optimizer infers from the conversation alone.
  • Change kind to "metaprompt" with config={"max_reflection_steps": 1} and compare.

Little by little, you're building something great.