Skip to content

Hooks

abmind integrates with AI coding agents via lifecycle hooks. When the agent reaches a specific point in its workflow, it fires a hook — abmind intercepts it to inject memories, extract knowledge, or log context.

Supported Events

All 12 Claude Code lifecycle events are covered:

EventWhen it firesWhat abmind does
PreUserPromptBefore user message reaches modelInjects relevant memories (active recall)
PreToolUseBefore a tool executesSecurity gate — blocks unauthorized writes
PostToolUseAfter tool succeedsCaptures tool context to sidecar
PostToolUseFailureAfter tool failsStores novel failure patterns as lessons (24h dedup)
PreCompactBefore context compactionExtracts memories from transcript before it's lost
PostCompactAfter context compactionInjects fresh recall to re-ground the agent
PostUserPromptAfter model respondsReal-time extraction on high-signal exchanges
SubagentStartSubagent spawnedLogs delegation as session breadcrumb
SubagentStopSubagent finishesCaptures subagent output as memory
NotificationAgent shows notificationLogs as session breadcrumb
AgentSpawnAgent session startsWakeup recall — injects identity + recent context
StopSession endsFinal memory flush — stores the last exchange

How it works

Each hook is a standalone Node.js script in cli/abmind-hook-*.ts. The agent config (agents/abmind.json) maps events to commands:

json
{
  "hooks": {
    "PreUserPrompt": [{"command": "node dist/cli/abmind-hook-preuserprompt.js"}],
    "PreToolUse": [{"command": "node dist/cli/abmind-hook-preToolUse.js", "matcher": "memory_store"}],
    "Stop": [{"command": "node dist/cli/abmind-hook-store.js"}]
  }
}

Input/Output protocol

  • Input: Hook receives JSON on stdin (event-specific payload)
  • Output: Hook writes to stdout (injected into agent context)
  • Exit codes: 0 = success, 2 = skip (don't inject output)

Key behaviors

PreUserPrompt (active recall)

Fires on every user message. Extracts English tokens from the prompt, runs a multi-query recall (FTS + vector + entity graph), returns top matches capped at 2000 chars. Secrets (classification=3) are never surfaced.

PreCompact (memory preservation)

The most critical hook. When the context window fills up and the agent compacts, this hook fires FIRST — extracting memories from the transcript that's about to be discarded. Without it, long conversations lose knowledge.

PostUserPrompt (real-time extraction)

Uses a heuristic gate — only fires the extraction pipeline when the exchange contains high-signal patterns:

  • "remember this", "I prefer", "I decided", "my X is Y"
  • Secrets/credentials mentioned
  • High emotion score

Cost: ~1 LLM call per 10-20 messages (not every exchange).

PostToolUseFailure (learning from mistakes)

Stores failure patterns as "lesson" memories. Deduplicates: same tool+error within 24h is stored only once. Prevents noise from flaky networks while capturing genuinely novel failures.

Security (PreToolUse)

Gates memory_store calls — validates classification level, blocks unauthorized writes to high-classification memories. Configurable via matcher field.

Configuration

Disable all hooks

bash
export ABMIND_HOOKS_DISABLED=true

Per-hook env vars

VariableDefaultDescription
ABMIND_HOOKS_DISABLEDfalseKill switch for all hooks
ABMIND_HOOK_RECALL_LIMIT5Max memories returned by PreUserPrompt
ABMIND_HOOK_RECALL_MAX_CHARS2000Max output chars for recall injection

Installation

Hooks ship with abmind. After abmind install, copy the agent config to your CLI's agents directory:

bash
# For Kiro CLI
cp node_modules/abmind/agents/abmind.json ~/.kiro/agents/

# For Claude Code
cp node_modules/abmind/agents/abmind.json ~/.claude/agents/

Or reference it in your agent configuration. The hooks resolve paths relative to the abmind package.

Compatibility

Agent HostSupportedNotes
Kiro CLIFull 12-event support
Claude CodePostCompact pending official support
Gemini CLIMaps to BeforeAgent/AfterAgent
CodexPartialPreToolUse + PostToolUse only

Troubleshooting

Hook not firing: Check agents/abmind.json is in the correct directory for your CLI. Verify with cat ~/.kiro/agents/abmind.json | jq '.hooks | keys'.

Slow response: Hooks add latency (DB query per message). If recall takes >500ms, check ~/.abmind/memory/memory.db size and run abmind doctor.

No memories returned: The DB may be empty. Send a few messages first, then trigger sleep (abmind sleep) to extract memories.