latch-compact

A command that summarizes the current coding-agent session into latch’s per-project knowledge base. It runs separately in the background so the session stays usable.

In plain words
What is it for?
Use it to compact the current session, or specify another session to compact, while continuing to work.
Why use it?
It preserves the latest session state without making the active conversation wait for the summary to finish.

Command

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/open-latch/latch/latch-compact
Clone the repo
git clone --depth 1 https://github.com/open-latch/latch
Per session 11 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 560 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00011 $0.00560
Opus 5 $0.00005 $0.00280
Sonnet 5 $0.00002 $0.00112
Haiku 4.5 $0.00001 $0.00056

Measured 2d ago against content hash e0d4f2590f46, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

latch-compact scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

commands/latch-compact.md · 56 lines

What it actually says

Run a compaction pass for the current Claude Code session against the per-project knowledge base under <KB_HOME>.

The compaction runs in a separate claude -p subprocess that takes its own context — it does NOT consume tokens from this session. We launch it in the background so the main session stays interactive while compaction runs.

This combines/overwrites the prior session_summary node with the latest state (see src/compactor.py).

Steps:

  1. The wrapper targets the invoking session automatically: it reads $CLAUDE_CODE_SESSION_ID (set by Claude Code on the Bash subprocess) and selects that session's transcript explicitly — a concurrent session's newer transcript is never picked. An explicit session id can be passed as the first positional arg to compact a different session.

  2. Launch the compactor with the Bash tool in the background so the main session is not blocked while compaction runs (a full compact can take 30–90s):

    Bash(
      command="bash <KB_HOME>/bin/run_compact_now.sh",
      run_in_background=true,
      description="Run KB compaction in background"
    )
    

    Return control to the user immediately with the background shell id. Do not poll — the harness will notify you when the subprocess exits. Omit --final — manual /latch-compact is a rolling compact, not a session end. The session_summary node stays in staging status.

  3. While the compactor is running, the project lock at <KB_HOME>/projects/<sanitized-cwd>/compactor.lock is held. Any latch_insert / latch_update / latch_link / latch_unlink calls made by the main session will block for up to 60s waiting for the compactor to finish, then return {"ok": false, "reason": "compaction_in_progress", "retry_after_s": 10} if it still hasn't finished. This is the deterministic write-side serialization — the busy payload's message tells you what to do: retry the write once, and if it is still locked, ask the user whether to retry again or investigate. Don't chase the lock.

  4. When the harness notifies that the background shell completed, fetch its stdout and report which summary_node_id was updated and a one-line gist.

If anything fails, check <KB_HOME>/compactor.log.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 56 lines · 11 tokens per session scan A e0d4f2590f46

Subscribe to this mod's changes

latch-compact is a command published in the GitHub repository open-latch/latch (9 stars, last pushed 2d ago), licensed Apache-2.0. It adds 11 tokens to every session and 560 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.