Skip to content
Agent Memory
Hook

Hooks

What ai-memory runs automatically, and when. A hook is a command Claude Code fires at a fixed moment, without you asking for it.

From plugin
ai-memory
16k7 skills1 hook1 MCP

Claude Code

> /plugin marketplace add akitaonrails/ai-memory
> /plugin install ai-memory@ai-memory

Where it lives

  • hooks/_lib.shGitHub
    Read the script
    # ai-memory hook helper — find marker file + parse minimal TOML.
    # Sourced by per-agent lifecycle hook scripts. POSIX shell only —
    # no bash-isms, no non-standard deps (no jq, no toml crate). Keep changes
    # byte-trivial because every supported agent (claude-code, codex,
    # cursor, gemini-cli, kimi-code, kiro-cli, antigravity-cli, opencode,
    # omp, pool) sources this same file.
    
    # Set `_amhome` to $HOME without trailing slashes ("/" stays "/"), so the
    # string patterns below treat `HOME=/home/u/` as the native walk does.
    ai_memory_home() {
        _amhome="${HOME:-}"
        while [ "${_amhome%/}" != "$_amhome" ] && [ -n "${_amhome%/}" ]; do
            _amhome="${_amhome%/}"
        done
    }
    
    # Walk up from "$1" toward $HOME (or /) looking for `.ai-memory.toml`.
    # Prints the absolute path of the first marker found, or nothing.
    # Stops at $HOME to avoid leaking declarations from a shared system
    # user's home into another user's session on multi-user boxes. When cwd is
    # outside HOME, stop at the nearest checkout root (`.git` file/dir); a plain
    # non-git directory checks only cwd. This keeps unrelated parent markers out.
    ai_memory_find_marker() {
        dir="$1"
        [ -z "$dir" ] && return 0
        boundary=""
        ai_memory_home
        if [ -n "$_amhome" ]; then
            case "$dir" in
                "$_amhome"|"${_amhome%/}"/*) boundary="$_amhome" ;;
                *)
                    probe="$dir"
                    while [ -n "$probe" ] && [ "$probe" != "/" ]; do
                        if [ -e "$probe/.git" ]; then
                            boundary="$probe"
                            break
                        fi
                        parent=$(dirname "$probe")
                        [ "$parent" = "$probe" ] && break
                        probe="$parent"
                    done
                    [ -n "$boundary" ] || boundary="$dir"
                    ;;
            esac
        fi
        while [ -n "$dir" ] && [ "$dir" != "/" ]; do
            if [ -f "$dir/.ai-memory.toml" ]; then
                printf '%s\n' "$dir/.ai-memory.toml"
                return 0
            fi
            if [ -n "$boundary" ] && [ "$dir" = "$boundary" ]; then
                return 0
            fi
            parent=$(dirname "$dir")
            [ "$parent" = "$dir" ] && return 0
            dir="$parent"
        done
    }
    
    # Parse `key = "value"` at the TOML root (no nesting, no arrays, no
    # tables). Returns the first match or nothing. Ignores comments and
    # blank lines by construction (the regex only matches the `key = "..."`
    # shape).
    ai_memory_strip_utf8_bom() {
        # Byte spelling keeps this portable across GNU/BSD sed and the awk images
        # used in CI; LC_ALL=C makes the three bytes ordinary pattern characters.
        LC_ALL=C sed "1s/^$(printf '\357\273\277')//"
    }
    
    ai_memory_parse_toml_key() {
        file="$1"; key="$2"
        [ -f "$file" ] || return 0
        ai_memory_strip_utf8_bom <"$file" \
            | sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"([^\"]*)\".*/\1/p" \
            | head -n 1
    }
    
    # Like ai_memory_parse_toml_key but also accepts a BARE value
    # (`key = true` / `key = 6000`), so section-style flags such as
    # `[briefing] inject_on_session_start = true` work quoted or not.
    # Parity with `parse_toml_flag` in hook_capture.rs: line-based (section
    # headers are ignored), first match wins, trailing `# comment` stripped.
    ai_memory_parse_toml_flag() {
        file="$1"; key="$2"
        [ -f "$file" ] || return 0
        ai_memory_strip_utf8_bom <"$file" \
            | sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"?([^\"#]*)\"?.*/\1/p" \
            | head -n 1 | sed 's/[[:space:]]*$//'
    }
    
    # Section-scoped counterpart for the same-named [handoff] and [recovery]
    # delivery keys. The exclusion mode preserves legacy handoff parsing elsewhere.
    ai_memory_parse_toml_scoped_flag() {
        file="$1"; section="$2"; key="$3"; exclude="$4"
        [ -f "$file" ] || return 0
        ai_memory_strip_utf8_bom <"$file" | LC_ALL=C awk \
            -v section="$section" -v key="$key" -v exclude="$exclude" '
            function trim(value) {
                gsub(/^[ \t\r]+|[ \t\r]+$/, "", value)
                return value
            }
            {
                header = $0
                sub(/#.*/, "", header)
                header = trim(header)
                if (header ~ /^\[.*\]$/) {
                    in_section = trim(substr(header, 2, length(header) - 2)) == section
                    next
                }
                if ((exclude == "1" && in_section) || (exclude != "1" && !in_section)) next
                pattern = "^[ \t]*" key "[ \t]*="
                if ($0 !~ pattern) next
                value = $0
                sub(pattern, "", value)
                sub(/#.*/, "", value)
                value = trim(value)
                gsub(/^"|"$/, "", value)
                if (value != "") {
                    print value
                    exit
                }
            }'
    }
    
    ai_memory_parse_toml_section_flag() {
        ai_memory_parse_toml_scoped_flag "$1" "$2" "$3" 0
    }
    
    ai_memory_parse_toml_flag_outside_section() {
        ai_memory_parse_toml_scoped_flag "$1" "$2" "$3" 1
    }
    
    # A resolved marker's `[profile]` flag as the explicit value the hook sends:
    # 0 for a falsy value (0/false/no/off, any case), 1 for anything else, an
    # absent key included. Parity with `profile_flag_value` in hook_capture.rs.
    ai_memory_profile_flag() {
        case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
            0 | false | no | off) printf '0' ;;
            *) printf '1' ;;
        esac
    }
    
    # Whether "$1" (a marker file) declares anything beyond a `[capture]`
    # section: any root-level scope key (workspace/project/project_strategy), or
    # any of the other settings ai_memory_marker_qs / ai_memory_briefing_qs
    # forward (drop_subagent_captures, default_global, [briefing], [profile],
    # [handoff] and [messaging] keys). Mirrors
    # `declares_more_than_capture` in marker.rs. A marker with any of these is a
    # resolution boundary; only a marker whose only content is `[capture]` (e.g.
    # ignore_paths) is scope/settings-transparent (#668).
    ai_memory_marker_declares_settings() {
        file="$1"
        [ -f "$file" ] || return 1
        for key in workspace project proje

Read the script before you install anything that runs on your machine. This is the one part of a plugin that acts without being asked.

@akitaonrails
Ships withai-memory

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

16k7 skills1 MCP
Get the whole plugin

Stars on GitHub

Momentum

stars in total
16.4k
Star history of akitaonrails/ai-memory on GitHub, a chart by star-history.com

More plugins that ship hooks.

mempalace

mempalace

Gives Claude Code a private memory that stays on your machine. It stores past conversations word for word, so you can ask what was decided, what happened before, or who was involved and get the original context back. It can also hand tasks to other agents.

3 skills+361k
@milla-jovovich@milla-jovovich
Install Plugin
mempalace

mempalace

Local-first memory for Claude Code. It stores conversations verbatim in a private palace you can search before answering about past work, people, projects, or decisions, with optional shared-brain hub setup.

3 skills+361k
@mempalace@mempalace
Install Plugin
context-mode

context-mode

Keeps large command outputs, logs and data files out of your AI assistant's context by running processing in a sandbox and returning only the relevant parts. You also get a searchable index of local files and diagnostics for checking the setup.

4 skills4 hooks35k
@mksglu@mksglu
Install Plugin
context-mode

context-mode

Keeps large tool outputs, logs, and files out of Claude Code's context window by processing them in a sandbox and indexing them for focused search. Includes diagnostics and a knowledge base indexer.

4 skills4 hooks34k
@mksglu@mksglu
Install Plugin
letta-code

letta-code

A coding agent harness whose agents keep memory and identity across sessions, so they learn and improve over time. You can run it in a terminal, in the browser, or through Telegram, Slack and Discord.

26 skills8 hooks3.9k
@letta-ai@letta-ai
Install Plugin
token-optimizer

token-optimizer

Audits a Claude Code or Codex setup for context-window waste, fixes it with the user's approval, and measures the savings. It also tracks costs, helps resume prior sessions, and offers a usage dashboard.

5 skills+23.2k
@alexgreensh@alexgreensh
Install Plugin
claude-subconscious

claude-subconscious

A background agent for Claude Code that watches your sessions and reads your files, builds up memory over time, and offers guidance back during your work.

4 hooks2.9k
@letta-ai@letta-ai
Install Plugin
claude-reflect

claude-reflect

A self-learning system for Claude Code that captures your corrections during sessions and reminds you to run /reflect to save them to CLAUDE.md as permanent memory. It also discovers recurring workflow patterns.

1 skill+22k
@bayramannakov@bayramannakov
Install Plugin

Questions about the hooks in ai-memory.

Which coding agents can use the hooks in ai-memory?
The hooks in ai-memory install in Claude Code and Cursor: each runs Claude Code hooks from .claude/settings.json, so copy the hooks there.