CommunityWriting & Editinggithub.com

totwo2/nm-agents

AI Agent runtime kernel with first-class persistent memory. Survives compression and model switches with layered memory, forced re-ingestion, intelligent routing, type-aware compression, and self-evolution. Zero default LLM dependency.

What is nm-agents?

nm-agents is a pure Python AI Agent runtime kernel that provides a complete agent harness including main loop, task routing, model management, context management, permissions, tool registration, self-evolution, and persistent memory. Its standout feature is a first-class memory system that survives conversation compression and model switches—two common scenarios that cause other systems to lose context. The memory operates in layers: anchor layer (decisions, facts) stays in system prompts continuously, while detailed transcripts are re-ingested on demand. The system supports intelligent task routing between local and cloud models based on signals, type-aware compression to reduce token usage without losing critical information, and a self-evolution pipeline for continuous improvement. As a pure library with no built-in tools or network endpoints, nm-agents requires the integrating party to register their own business tools and provide LLM configuration as needed. It supports platforms like Claude Code, Cursor, and Codex through its open adapter pattern, making it suitable for developers building custom agent infrastructures that require reliable memory across sessions.

Works with~Claude Code✓Codex CLI~Cursor
npx skills add totwo2/nm-agents

Installed? Explore more Writing & Editing skills: steipete/notion, langchain-ai/langchain, bytedance/podcast-generation · View all 6 →

Ask in your favorite AI

Open a new chat with this agent skill pre-loaded.

Documentation

What does nm-agents do?

A pure Python AI Agent runtime kernel: main loop + routing + model management + context + permissions + tool registration + self-evolution + persistent memory. Does not include any business tools—those are registered by the integrating party as needed.

When to Use

  • When you need to run an agent main loop (input → memory re-ingestion → route model → call LLM → execute tools → consolidate memory)
  • When you need persistent memory (cross-session decisions/facts/trajectories, surviving compression and model switches)
  • When you need task routing (local/cloud model selection based on signals), permission checking, and a self-evolution pipeline
  • It is a pure library: tools are registered by the integrating party

Installation

pip install -e .
# Dependencies: openai>=2.0, pydantic>=2.0, requests>=2.30 (Python >= 3.10)

Minimal Setup (AgentLoop)

from nm.config import HarnessConfig, DEFAULT_SYSTEM_PROMPT
from nm.agent_loop import AgentLoop
from nm.memory.memory_adapter import MemoryAdapter
from nm.context_manager import ContextManager
from nm.permission_checker import PermissionChecker
from nm.tool_registry import ToolRegistry

config = HarnessConfig(stream_output=False)
mem = MemoryAdapter("memory_store.json", "transcript.jsonl")
ctx = ContextManager(DEFAULT_SYSTEM_PROMPT, workspace_dir=".")
router = TaskRouter(model_manager)          # Must construct ModelManager first and register accounts/groups
reg = ToolRegistry().create_default()        # Only includes get_tool_detail; register your business tools yourself
perm = PermissionChecker(mode=PermissionMode.AUTO)

harness = AgentLoop(config, mem, router, reg, ctx, perm)
result = harness.run("Help me organize this data", user_id="user1")

run() returns {text, tool_calls, usage, model, turns}.

Memory API (MemoryAdapter)

  • ingest(role, content, scope) / distill(scope): Write conversation + distill into structured memory
  • recall(scopes): Full re-ingestion as a single text block
  • recall_anchors(scopes): Anchor layer (decisions + facts), typically kept in system prompt
  • recall_detail(scopes): Full detail (trajectory transcript), re-ingested on demand
  • capture({layer, ...}): Explicitly capture a structured memory entry
  • mark_used(ids) / stats(): Read receipts and statistics

Memory closure: ContextManager calls the on_compaction callback during compression to consolidate the compressed original text into anchors; AgentLoop supplements full details when switching models—both memory loss scenarios (compression and model switch) are covered.

Configuration

  • Memory defaults to .nm/ in the run directory (use config_dir or other explicit paths; writable directory required)
  • LLM endpoint (only needed for semantic extraction): Environment variables OPENAI_BASE_URL/OPENAI_API_KEY → llm_config.json → fallback to a local compatible gateway; default heuristic extraction requires no dependencies

Verification

python test_selfverify.py    # 22 assertions, covering 4 memory closure scenarios

Notes

  • Permissions default to ASK; without a callback, execution is denied—the integrating party must provide an ask_callback or change to AUTO
  • Tools must be explicitly registered; unregistered tools will be rejected
  • Does not include any network service entry point; it is a pure library

Related Skills