# Give Your Python AI Agent Persistent Memory with One JSON File

DevFeed: [Give Your Python AI Agent Persistent Memory with One JSON File](<https://devfeed.tech/articles/give-your-python-ai-agent-persistent-memory-with-one-json-file-121287.md>)

Original publisher: [Read original article](<https://towardsdatascience.com/give-your-python-ai-agent-persistent-memory-with-one-json-file/>)

Author: Mahnoor Javed

Published: 2026-11-06T12:00:00Z

Content type: tutorial

Language: en

Sources: [Towards Data Science](<https://devfeed.tech/sources/towards-data-science.md>)

Topics: [AI Agent](<https://devfeed.tech/topics/ai-agent.md>), [Python](<https://devfeed.tech/topics/python.md>), [JSON](<https://devfeed.tech/topics/json.md>), [AI Chat](<https://devfeed.tech/topics/ai-chat.md>)

Tags: [agent](<https://devfeed.tech/tags/agent.md>), [agent-memory](<https://devfeed.tech/tags/agent-memory.md>), [agentic-ai](<https://devfeed.tech/tags/agentic-ai.md>), [agentmemory](<https://devfeed.tech/tags/agentmemory.md>), [ai](<https://devfeed.tech/tags/ai.md>), [ai-agent](<https://devfeed.tech/tags/ai-agent.md>), [ai-chatbots](<https://devfeed.tech/tags/ai-chatbots.md>), [ai-model](<https://devfeed.tech/tags/ai-model.md>), [aiagent](<https://devfeed.tech/tags/aiagent.md>), [api](<https://devfeed.tech/tags/api.md>), [api-key](<https://devfeed.tech/tags/api-key.md>), [calculator](<https://devfeed.tech/tags/calculator.md>), [classes](<https://devfeed.tech/tags/classes.md>), [editor-s-picks](<https://devfeed.tech/tags/editor-s-picks.md>), [file](<https://devfeed.tech/tags/file.md>), [json](<https://devfeed.tech/tags/json.md>), [memory](<https://devfeed.tech/tags/memory.md>), [openrouter](<https://devfeed.tech/tags/openrouter.md>), [persistent-memory](<https://devfeed.tech/tags/persistent-memory.md>), [python](<https://devfeed.tech/tags/python.md>), [recall](<https://devfeed.tech/tags/recall.md>), [web-search](<https://devfeed.tech/tags/web-search.md>)

## AI overview

The tutorial builds a simple Python agent whose conversation history persists in `memory.json`: `AgentMemory` loads and saves timestamped messages and recalls the last five for context. An `AIAgent` sends that context and the user's prompt to a model through the OpenAI client configured for OpenRouter, then recognizes tool requests using a `TOOL:` response prefix. Its `Tools` class provides a calculator and a `web_search` demonstration backed by two hard-coded results; the search does not access the web, and the article says a search API would be needed for real results. The example uses `.env` for the API key and describes changing the OpenRouter model if errors occur. The author notes memory is limited to recent conversations and suggests improving storage and retrieval.

## Source excerpt

Three Python classes, an OpenRouter key, and gpt-4.1-mini give the agent a calculator, a stub web search, and conversation recall that survives restarts. The post Give Your Python AI Agent Persistent Memory with One JSON File appeared first on Towards Data Science.