ExamplescriptintermediateRunnablememory-lab
Memory: Stores and retrieves relevant information across interactions.
This component maintains conversation history and context to enable coherent multi-turn interactions.
Key Facts
- Level
- intermediate • Agent Building Blocks
- Runtime
- Python • OpenAI API
- Pattern
- Memory-aware assistance with legible context
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
2-memory.py
python
"""
Memory: Stores and retrieves relevant information across interactions.
This component maintains conversation history and context to enable coherent multi-turn interactions.
More info: https://platform.openai.com/docs/guides/conversation-state?api-mode=responses
"""
from openai import OpenAI
client = OpenAI()
def ask_joke_without_memory():
response = client.responses.create(
model="gpt-4o-mini",
input=[
{"role": "user", "content": "Tell me a joke about programming"},
],
)
return response.output_text
def ask_followup_without_memory():
response = client.responses.create(
model="gpt-4o-mini",
input=[
{"role": "user", "content": "What was my previous question?"},
],
)
return response.output_text
def ask_followup_with_memory(joke_response: str):
response = client.responses.create(
model="gpt-4o-mini",
input=[
{"role": "user", "content": "Tell me a joke about programming"},
{"role": "assistant", "content": joke_response},
{"role": "user", "content": "What was my previous question?"},
],
)
return response.output_text
if __name__ == "__main__":
# First: Ask for a joke
joke_response = ask_joke_without_memory()
print(joke_response, "\n")
# Second: Ask follow-up without memory (AI will be confused)
confused_response = ask_followup_without_memory()
print(confused_response, "\n")
# Third: Ask follow-up with memory (AI will remember)
memory_response = ask_followup_with_memory(joke_response)
print(memory_response)
Related principles
- P1delegationDesign for delegation rather than direct manipulationDesign experiences around the assignment of work, the expression of intent, the setting of constraints, and the review of results, rather than requiring users to execute each step manually.Open principle →
- P5delegationReplace implied magic with clear mental modelsThe product should help users understand what the system can do, what it is currently doing, what it cannot do, and what conditions govern its behaviour.Open principle →
- P7trustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P8trustMake hand-offs, approvals, and blockers explicitWhen the system cannot proceed, the reason should be immediately visible, along with any action required from the user or another dependency.Open principle →
- P10delegationOptimise for steering, not only initiatingThe system should support users not only in starting tasks, but also in guiding, refining, reprioritising, and correcting work while it is underway.Open principle →