MadMax icon indicating copy to clipboard operation
MadMax copied to clipboard

Real-time voice AI agent with persistent memory, built on Gemini Live API and Python

๐Ÿ˜Ž MadMax Live Agent

A voice agent for devices, powered by Google Gemini Live API with a local wake-word detector and long-term memory.

Capabilities:

  • ๐Ÿ—ฃ๏ธ Realtime Speech-to-Speech dialogue via Gemini Live API
  • ๐Ÿ” Fresh information from the internet via Google Search (grounding)
  • ๐Ÿ’ฐ Budget-friendly: offline mode with local wake-word (Vosk) and auto-shutdown timer
  • ๐Ÿง  Managed long-term memory based on JSON files: people, places, facts, goals, experience, episodes, reflections and persona
  • ๐Ÿ› ๏ธ Tool calling in live mode

Technical highlights:

  • ๐Ÿ”’ Transactional memory isolation: backup + rollback on errors, single-use guard
  • ๐Ÿงช LLM Surgeon: automatic memory conflict resolution via LLM
  • ๐Ÿ“ Auto-save all sessions to daily markdown
  • ๐Ÿ”„ Automatic recovery of missed sessions
  • โฑ๏ธ Graceful shutdown with configurable timeouts
  • ๐Ÿ“Š Latency diagnostics for all LLM calls in logs

In development:

  • ๐Ÿ“ท Photo and video stream processing
  • ๐Ÿงน Smart long-term memory cleanup
  • ๐Ÿค– Integration with ROS2 modules for robot control
  • ๐Ÿ”ง Other integrations and improvements

๐Ÿš€ Key Features

๐Ÿ’ฌ Realtime Voice Loop

  • Local wake-word detection via Vosk (no LLM costs)
  • Instant transition to live mode on the wake word
  • Speech-to-Speech dialogue with minimal latency

๐Ÿง  Post-Session Memory Pipeline

  • Automatic extraction: facts, goals, experience, episodes, reflections and persona after every session
  • LLM Surgeon: memory conflict resolution (UPDATE / MERGE / APPEND / IGNORE) via a separate LLM call
  • Rebuild of active_context.json for upcoming dialogues
  • Automatic recovery of unprocessed sessions

๐ŸŽญ Agent Persona (Max)

  • Name, gender and communication style are set in agent_instructions.md
  • SOUL.md โ€” philosophical persona manifesto: attitude toward the world, inclinations, shadow, meta-reflection, written by the agent itself after hours of testing conversations
  • Automatic extraction of reflections and persona traits from dialogues into reflections.json

๐Ÿ—๏ธ Architecture

1๏ธโƒฃ Sleep Mode

Agent is offline; microphone is monitored locally via Vosk. No LLM costs.

2๏ธโƒฃ Active Session

After the wake word audio switches to Gemini Live API. Dialogue runs in realtime.

3๏ธโƒฃ Post-Session Processing

After a session ends:

  1. Save transcript to memory_engine/daily/YYYY-MM-DD.md
  2. Call process_missing_sessions(day_date) โ€” process all unprocessed sessions of the day
  3. Call build_memory_context() โ€” rebuild active context

๐Ÿ’พ Memory Structure

๐Ÿ“… Daily Markdown Logs

Every session is saved to memory_engine/daily/YYYY-MM-DD.md with metadata:

  • session_id
  • started_at and ended_at (ISO 8601 with timezone offset)
  • Dialogue transcript

Source of truth for post-session processing.

๐Ÿงฉ Active Context

File memory_engine/active_context.json:

{
  "last_context": "...",
  "summary_yesterday": "...",
  "summary_today": "...",
  "reply_count_today": 0,
  "summary_reply_count": 0,
  "long_term_injections": []
}

Used as the working context for upcoming live sessions.

๐Ÿ—„๏ธ Long-Memory Storage

Directory memory_engine/memory/:

  • people.json โ€” information about people
  • places.json โ€” places and locations
  • facts.json โ€” facts and knowledge
  • goals.json โ€” goals and tasks
  • experience.json โ€” experience and skills
  • reflections.json โ€” reflections and insights
  • episodes.log.jsonl โ€” episode chronology
  • processed_sessions.json โ€” registry of processed sessions (prevents reprocessing the same session)

โš™๏ธ Configuration

  • Google AI Studio API Key in .env
  • Key parameters in config.py
  • Memory settings in memory_config.py

๐Ÿ“‚ Project Structure

MadMax/
โ”œโ”€โ”€ main.py                          # Entry point
โ”œโ”€โ”€ config.py                        # Configuration
โ”œโ”€โ”€ agent_instructions.md            # Agent system prompt (Max)
โ”œโ”€โ”€ SOUL.md                          # Agent persona and philosophy
โ”œโ”€โ”€ core/
โ”‚   โ”œโ”€โ”€ orchestrator.py              # Agent lifecycle (sleep / live / post-session)
โ”‚   โ”œโ”€โ”€ audio_io.py                  # Audio I/O and Vosk wake-word
โ”‚   โ”œโ”€โ”€ gemini_client.py             # Gemini Live API client
โ”‚   โ”œโ”€โ”€ agent_tools.py               # Memory tools for live mode
โ”‚   โ”œโ”€โ”€ session_transcript_logger.py # Session transcript persistence
โ”‚   โ”œโ”€โ”€ errors.py                    # Exceptions
โ”‚   โ””โ”€โ”€ state.py                     # Session state
โ”œโ”€โ”€ memory_engine/
โ”‚   โ”œโ”€โ”€ active_context_builder.py    # active_context.json builder
โ”‚   โ”œโ”€โ”€ long_memory_extractor_agent.py  # Memory extraction from transcripts
โ”‚   โ”œโ”€โ”€ long_memory_apply.py         # Memory operations + LLM Surgeon
โ”‚   โ”œโ”€โ”€ long_memory_normalize.py     # Normalization and fuzzy matching
โ”‚   โ”œโ”€โ”€ long_memory_ops.py           # Operation schemas and validation
โ”‚   โ”œโ”€โ”€ long_memory_query_service.py # Long-term memory search
โ”‚   โ”œโ”€โ”€ summarize_context_agent.py   # Day summarization
โ”‚   โ”œโ”€โ”€ llm_client_utils.py          # LLM timeout and diagnostics
โ”‚   โ”œโ”€โ”€ memory_config.py             # Memory paths and constants
โ”‚   โ”œโ”€โ”€ entity_policies.py           # Entity link policies
โ”‚   โ”œโ”€โ”€ time_policy.py               # Timestamp policy
โ”‚   โ”œโ”€โ”€ daily/                       # Daily markdown logs
โ”‚   โ””โ”€โ”€ memory/                      # Long-memory JSON files + backups
โ””โ”€โ”€ live_api_docs/                   # Gemini Live API documentation

๐Ÿš€ Quick Start

# 1. Clone and enter directory
cd MadMax

# 2. Create virtual environment
python3 -m venv venv
source venv/bin/activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Download wake-word model (~40 MB)
./setup.sh

# 5. Configure environment
# Create .env file (or export variables):
# GOOGLE_API_KEY=your_key_here

# 6. Run the agent
python main.py

Requirements:

  • Linux
  • Python 3.11+
  • Microphone and speakers
  • Google AI Studio API Key

๐Ÿ› ๏ธ Roadmap

โœ… Already implemented

  • Function Calling for memory โ€” live agent calls memory_lookup_person, memory_lookup_goal, memory_lookup_experience, memory_recent_episodes during dialogue
  • Google Search (grounding) โ€” agent receives fresh information from the internet in realtime
  • Transactional memory isolation โ€” backup before write, rollback on errors, single-use guard for apply_payload
  • LLM Surgeon โ€” automatic memory conflict resolution via a separate LLM call with batching
  • Fail-fast error handling โ€” explicit logs on corrupted JSON, graceful CancelledError, latency diagnostics

๐ŸŽฏ Planned

๐Ÿงน Smart long-term memory cleanup

Goal: Automatic removal of stale or irrelevant data from memory.

Planned logic:

  • Fact prioritization โ€” relevance score based on access frequency and freshness
  • Old episode archival โ€” move rarely used episodes to cold storage
  • Automatic duplicate merging โ€” find and merge similar facts/goals
  • Temporary goal expiration โ€” auto-complete or archive goals with expired deadlines
  • Configurable retention rules โ€” set data lifetime for different categories

Result: Memory stays relevant, does not grow uncontrollably, and is not cluttered with duplicates and outdated information.

๐Ÿ”ง Refactoring & Type Safety

  • Pydantic for structured payloads instead of dict[str, Any]

๐Ÿ—๏ธ Technical Debt

Consciously accepted trade-offs that are known and documented:

Issue Impact Why we kept it
Any instead of Pydantic for operation payloads No type safety, IDE does not suggest fields It works, changing it requires rewriting 5+ modules
Tight coupling: GeminiLiveClient imports AudioIO Hard to test, risk of circular dependency No DI container, Protocols require refactoring
No CI/CD No automatic type checking and tests Project is developed locally, pytest is run manually

๐Ÿค– Agentic Engineering

Important: A significant part of this project was written using Agentic Engineering in pair-programming mode.


๐Ÿ“Š Current Status

The project consists of three stable loops:

  • Live conversation loop โ€” realtime Speech-to-Speech dialogue with the user (Google Search, tool calling, Vosk wake-word)
  • Post-session memory loop โ€” automatic extraction, deduplication and knowledge persistence (people, places, facts, goals, experience, episodes, reflections, persona)
  • Reliability loop โ€” transactional isolation (backup + rollback), graceful shutdown, LLM timeouts, recovery of missed sessions

The voice agent is ready for daily use as-is. The main constraints are architectural debt (see section above), not functional issues.