Persisting memory with the SDK

Agents become genuinely useful when they can remember more than a single interaction. In real systems, we often want an agent to carry context across runs, restarts, or sessions. The Agents SDK includes built-in support for persisting memory, so continuity does not have to be rebuilt from scratch each time an agent starts.

What kinds of memory the SDK can persist

The SDK can persist information that represents an agent’s conversational and operational state. This typically includes prior messages, summaries, or structured memory the agent has accumulated while running. The key idea is that this memory is owned and managed by the SDK, not manually reconstructed by our code.

Configuring memory persistence for an agent

Memory persistence is enabled through agent configuration. When defining an agent, we choose whether its memory should be transient or durable. This choice tells the SDK whether state should exist only for the current run or be written to a backing store for later reuse.

from openai import Agent

agent = Agent(
    name="planet_assistant",
    instructions="Answer questions about planets and moons.",
    memory={"type": "file", "path": "agent_memory.json"}
)

Saving agent memory across runs

Once persistence is configured, saving memory does not require explicit calls in most cases. As the agent processes input and produces output, the SDK updates its memory automatically. When the program exits cleanly, the current memory state is written to the configured storage.

Restoring memory when an agent restarts

When the agent is created again with the same persistence configuration, the SDK loads the previously saved memory. From the agent’s perspective, this feels like a continuation rather than a fresh start. Prior context is available immediately, without replaying earlier conversations.

agent = Agent(
    name="planet_assistant",
    instructions="Answer questions about planets and moons.",
    memory={"type": "file", "path": "agent_memory.json"}
)

Using persisted memory to maintain continuity

Persisted memory allows an agent to behave consistently over time. It can remember earlier decisions, ongoing tasks, or established context and use that information to guide future responses. This continuity is essential for long-running agents and for tools that are expected to feel stateful rather than reactive.

Conclusion

By enabling memory persistence, we move from disposable agents to continuous ones. The SDK handles saving and restoring state, allowing us to focus on behavior and capabilities instead of plumbing. At this point, we are oriented to how SDK-managed memory survives across runs and supports coherent agent behavior over time.