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

Prompt optimization: learning from feedback

Memory can also change how an agent behaves. create_prompt_optimizer reads conversations and feedback and returns an improved system prompt.

Example
optimizer = create_prompt_optimizer(MemoryModel(), kind="prompt_memory")
trajectory = [
    {"role": "user", "content": "Please answer me by email."},
    {"role": "assistant", "content": "Here is your answer in the chat."},
]
new_prompt = optimizer.invoke({
    "prompt": "You answer support tickets for an online shop.",
    "trajectories": [(trajectory, "Use the contact method the customer asks for.")],
})
print(new_prompt)

A trajectory is a conversation, paired with feedback on it. The optimizer shows the model the current prompt, every trajectory and its feedback, and asks for the full updated prompt. The stand-in's improve_prompt only appends the feedback as a rule; a real model rewrites the prompt so the rule fits in.

Three kinds

kindModel callsHow it works
prompt_memory1One request: current prompt, trajectories and feedback in, new prompt out.
metapromptUp to max_reflection_steps + 1Reflects on the trajectories, possibly several times, before writing the prompt.
gradient2 or moreFirst writes a critique of what went wrong, then applies it as edits to the prompt.

Only prompt_memory is run here: the other two ask the model for open-ended critique and reflection, which only a real model produces usefully. Their code is identical apart from kind.

Review before you ship
An optimized prompt is a new version of your agent. Keep it in version control, and run your evals, as in the DeepEval course, before switching to it.
Try it yourself
  • Pass two trajectories with different feedback.
  • Pass feedback of an empty string and compare the result.
  • Use create_multi_prompt_optimizer in LangMem's reference for an agent with several prompts.

Little by little, you're building something great.