Skip to main content
AFK’s memory system persists conversation state across runs. Use it for multi-turn conversations, run resumption after interrupts, long-term knowledge retention, and vector-based semantic search.

Quick start: multi-turn conversation

The thread_id links runs into a conversation. AFK automatically persists messages between runs.

What gets stored

What’s NOT stored automatically: Raw LLM provider responses or internal framework temporaries. Only conversation-visible records and explicit state writes are persisted.

State lifecycle

Resume interrupted runs

If a run is interrupted (crash, timeout, pause for approval), resume from the last checkpoint:
Checkpoints are written at key boundaries: before each LLM call, after each tool batch, and after each step completes. On resume, completed tool calls are replayed from the effect journal — no duplicate side effects.

Compact long threads

Over time, conversation threads grow and consume tokens. Use compaction to trim old events:
Compaction applies retention rules: protected event types are preserved first, then the most recent remaining events fill the budget.

Memory backends

AFK ships with four backends. All implement the MemoryStore protocol.
State lives in process memory. Fast, no setup, but lost on restart.
Use for: Development, testing, short-lived scripts.

Connection pooling for Redis

For production Redis deployments, use connection pooling for better performance:

Environment-based selection

Set environment variables to auto-select a backend without code changes:
The runner falls back to in-memory if the configured backend fails to initialize.

Custom backends

Implement the MemoryStore abstract class to add support for any database:
Declare capabilities to tell the framework which features your backend supports. Features like vector search are only used when the backend declares support.

Long-term memory

Beyond conversation events, AFK supports persistent long-term memories scoped per user and purpose:
Backends that support vector search (SQLite, Postgres) can find semantically similar memories:
All backends support basic text search across memory content:

Design guidelines

  • Always use thread_id for conversations. Without it, each run starts fresh.
  • Compact threads proactively. Don’t wait until you hit token limits. A good rule: compact when the thread exceeds ~500 events.
  • Use checkpoints for long-running agents. If a run might take minutes, checkpoints let you resume on failure.
  • Don’t store secrets in memory. Thread events are persisted and may be readable.
  • Choose the right backend. In-memory for dev, SQLite for local persistence, Postgres/Redis for production.
  • Use scopes for long-term memory. Organize memories by purpose (preferences, knowledge, history) to keep queries efficient.

Next steps

Core Runner

Resume and compact APIs on the Runner.

System Prompts

Template prompts with context from memory.