# In-Memory Storage for Agents (/database/providers/in-memory/usage/in-memory-for-agent)



`InMemoryDb` stores an Agent's sessions and run history in the current Python process. Data is lost when the process exits.

## Usage [#usage]

Start in a [virtual environment](/sdk/setup) and set the model key before running the example.

## Set OpenAI Key [#set-openai-key]

Set your `OPENAI_API_KEY` as an environment variable. You can get one [from OpenAI](https://platform.openai.com/account/api-keys).

<CodeBlockTabs defaultValue="Mac">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Mac">
      Mac
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Windows PowerShell">
      Windows PowerShell
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Mac">
    ```bash
    export OPENAI_API_KEY=sk-***
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Windows PowerShell">
    ```powershell
    $env:OPENAI_API_KEY = "your-openai-api-key"
    ```
  </CodeBlockTab>
</CodeBlockTabs>

Install dependencies:

```shell
uv pip install agno openai
```

```python
from agno.agent import Agent
from agno.db.in_memory import InMemoryDb

db = InMemoryDb()

agent = Agent(db=db, add_history_to_context=True)

agent.print_response("Give me an easy dinner recipe")
agent.print_response("Which ingredients did you just recommend?")
```

## Run the Example [#run-the-example]

Save the code as `in_memory_for_agent.py`, complete the prerequisites above, then run:

```bash
python in_memory_for_agent.py
```
