A Self-Updating Knowledge Base for Terminal AI

I spend most of my day in the terminal with an AI coding assistant.

I often solve hard problems during a session. I find a tricky fix or a specific config setting. Then I close the tab and the knowledge vanishes. A month later, I solve the exact same problem again.

I built a system to make my assistant maintain its own notes using Claude Code hooks.

The system uses three parts:

  • Search a small Markdown knowledge base on every prompt to provide context.
  • Capture useful notes when a session ends.
  • Load the index when a session starts.

Here is how to build it without slowing down your workflow.

  1. Use UserPromptSubmit for retrieval

This hook runs every time you send a prompt. It receives your text and can inject context before the model responds.

Do not let the model decide when to look at your notes. Use a fast search like grep to find relevant files. Inject only the top five matches. This keeps the process under 100ms.

Two rules for search hooks:

  • Keep it cheap. Use grep instead of an LLM for the initial search.
  • Keep it lean. Only inject titles and file paths. The model can open the file if it needs more detail.
  1. Avoid the Stop hook for heavy tasks

The Stop hook runs every time the assistant finishes a reply. If you run a long task here, your assistant becomes slow. A 30-turn session will trigger 30 heavy tasks.

Instead, use SessionEnd to capture the full session once.

  1. Solve the SessionEnd limitation

SessionEnd is non-blocking. If you start a long background task here, the system might kill it when the session closes.

The fix is to use two hooks:

  • SessionEnd: Quickly add the transcript path to a queue file.
  • SessionStart: Drain that queue and run the heavy capture task in the background.

Because the next session is active, the background process will survive.

  1. Prevent infinite loops

When you spawn a headless AI to write notes, it inherits your environment. This means the new AI will trigger its own hooks. This creates a loop of endless AI calls.

Always add a recursion guard at the top of your scripts:

[ -n "$KB_CAPTURE" ] && exit 0

Set KB_CAPTURE=1 when you launch your background capture task.

Summary of the setup:

  • Use UserPromptSubmit for instant context injection.
  • Use SessionEnd to enqueue tasks.
  • Use SessionStart to run the actual work.
  • Use a cheap model for summarizing notes to save money.

One avoided re-solve pays for many captures.

Source: https://dev.to/just_an_electron/a-self-updating-knowledge-base-for-my-terminal-ai-assistant-claude-code-hooks-28jb

Optional learning community: https://t.me/GyaanSetuAi