c648d5d8d2
* feat: add knowledge agent types, store, builder, and renderer Phase 1 of Knowledge Agents feature. Introduces corpus compilation pipeline that filters observations from the database into portable corpus files stored at ~/.claude-mem/corpora/. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add corpus CRUD HTTP endpoints and wire into worker service Phase 2 of Knowledge Agents. Adds CorpusRoutes with 5 endpoints (build, list, get, delete, rebuild) and registers them during worker background initialization alongside SearchRoutes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add KnowledgeAgent with V1 SDK prime/query/reprime Phase 3 of Knowledge Agents. Uses Agent SDK V1 query() with resume and disallowedTools for Q&A-only knowledge sessions. Auto-reprimes on session expiry. Adds prime, query, and reprime HTTP endpoints to CorpusRoutes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add MCP tools and skill for knowledge agents Phase 4 of Knowledge Agents. Adds build_corpus, list_corpora, prime_corpus, and query_corpus MCP tools delegating to worker HTTP endpoints. Includes /knowledge-agent skill with workflow docs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: handle SDK process exit in KnowledgeAgent, add e2e test The Agent SDK may throw after yielding all messages when the Claude process exits with a non-zero code. Now tolerates this if session_id/answer were already captured. Adds comprehensive e2e test script (31 assertions) orchestrated via tmux-cli. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: use settings model ID instead of hardcoded model in KnowledgeAgent Reads CLAUDE_MEM_MODEL from user settings via getModelId(), matching the existing SDKAgent pattern. No more hardcoded model assumptions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: improve knowledge agents developer experience Add public documentation page, rebuild/reprime MCP tools, and actionable error messages. DX review scored knowledge agents 4/10 — core engineering works (31/31 e2e) but the feature was invisible. This addresses discoverability (docs, cross-links), API completeness (missing MCP tools), and error quality (fix/example fields in error responses). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: add quick start guide to knowledge agents page Covers the three main use cases upfront: creating an agent, asking a single question, and starting a fresh conversation with reprime. Includes keeping-it-current section for rebuild + reprime workflow. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address code review issues — path traversal, session safety, prompt injection - Block path traversal in CorpusStore with alphanumeric name validation and resolved path check - Harden system prompt against instruction injection from untrusted corpus content - Validate question field as non-empty string in query endpoint - Only persist session_id after successful prime (not null on failure) - Persist refreshed session_id after query execution - Only auto-reprime on session resume errors, not all query failures - Add fenced code block language tags to SKILL.md Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address remaining code review issues — e2e robustness, MCP validation, docs - Harden e2e curl wrappers with connect-timeout, fallback to HTTP 000 on transport failure - Use curl_post wrapper consistently for all long-running POST calls - Add runtime name validation to all corpus MCP tool handlers - Fix docs: soften hallucination guarantee to probabilistic claim - Fix architecture diagram: add missing rebuild_corpus and reprime_corpus tools Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: enforce string[] type in safeParseJsonArray for corpus data integrity Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add blank line before fenced code blocks in SKILL.md maintenance section Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
208 lines
8.3 KiB
Plaintext
208 lines
8.3 KiB
Plaintext
---
|
|
title: "Knowledge Agents"
|
|
description: "Build queryable AI brains from your observation history"
|
|
---
|
|
|
|
# Knowledge Agents
|
|
|
|
Knowledge agents let you compile a slice of your claude-mem observation history into a **queryable "brain"** that answers questions conversationally. Instead of getting raw search results back, you get synthesized, grounded answers drawn from your actual project history -- decisions, discoveries, bugfixes, and features.
|
|
|
|
## Quick Start
|
|
|
|
Three ways to use knowledge agents, from simplest to most powerful.
|
|
|
|
### 1. Create a Knowledge Agent
|
|
|
|
Use the `/knowledge-agent` skill or the MCP tools directly:
|
|
|
|
```
|
|
build_corpus name="hooks-expertise" query="hooks architecture" project="claude-mem" limit=200
|
|
```
|
|
|
|
This searches your observation history, collects matching records, and saves them as a corpus file. Then prime it — this loads the corpus into a Claude session's context window:
|
|
|
|
```
|
|
prime_corpus name="hooks-expertise"
|
|
```
|
|
|
|
Your knowledge agent is ready. The returned `session_id` **is** the agent — a Claude session with your history baked in.
|
|
|
|
### 2. Ask a Single Question
|
|
|
|
Once primed, ask any question and get a grounded answer:
|
|
|
|
```
|
|
query_corpus name="hooks-expertise" question="What are the 5 lifecycle hooks and when does each fire?"
|
|
```
|
|
|
|
The agent answers grounded in its corpus — responses are drawn from your actual project history, reducing hallucination and guessing. Each follow-up question builds on the prior conversation:
|
|
|
|
```
|
|
query_corpus name="hooks-expertise" question="Which hook handles context injection?"
|
|
```
|
|
|
|
### 3. Start a Fresh Conversation
|
|
|
|
If the conversation drifts, or you want to ask an unrelated question against the same corpus, reprime to start clean:
|
|
|
|
```
|
|
reprime_corpus name="hooks-expertise"
|
|
```
|
|
|
|
This creates a **new session** with the full corpus reloaded — like opening a fresh chat with the same "brain." All prior Q&A context is cleared, but the corpus knowledge remains. Use this when:
|
|
|
|
- The conversation went off-track and you want a clean slate
|
|
- You're switching topics within the same corpus
|
|
- You want to ask a question without prior answers biasing the response
|
|
|
|
### Keeping It Current
|
|
|
|
When new observations are added to your project, rebuild the corpus to pull in the latest, then reprime:
|
|
|
|
```
|
|
rebuild_corpus name="hooks-expertise"
|
|
reprime_corpus name="hooks-expertise"
|
|
```
|
|
|
|
Rebuild re-runs the original search filters. Reprime loads the refreshed data into a new session.
|
|
|
|
---
|
|
|
|
## The Workflow: Build, Prime, Query
|
|
|
|
```
|
|
BUILD ──> PRIME ──> QUERY
|
|
```
|
|
|
|
### 1. Build a Corpus
|
|
|
|
A corpus is a filtered collection of observations saved as a JSON file. Use search filters to select exactly the slice of history you want.
|
|
|
|
```bash
|
|
curl -X POST http://localhost:37777/api/corpus \
|
|
-H "Content-Type: application/json" \
|
|
-d '{
|
|
"name": "hooks-expertise",
|
|
"query": "hooks architecture",
|
|
"project": "claude-mem",
|
|
"types": ["decision", "discovery"],
|
|
"limit": 200
|
|
}'
|
|
```
|
|
|
|
Under the hood, `CorpusBuilder` searches your observations, hydrates full records, parses structured fields (facts, concepts, files), calculates stats, and writes everything to `~/.claude-mem/corpora/hooks-expertise.corpus.json`.
|
|
|
|
### 2. Prime the Knowledge Agent
|
|
|
|
Priming loads the entire corpus into a Claude session's context window.
|
|
|
|
```bash
|
|
curl -X POST http://localhost:37777/api/corpus/hooks-expertise/prime
|
|
```
|
|
|
|
The agent renders all observations into full-detail text and feeds them to the Claude Agent SDK. Claude reads the corpus and acknowledges the themes. The returned `session_id` **is** the knowledge agent -- a Claude session with your history baked in.
|
|
|
|
### 3. Query
|
|
|
|
Resume the primed session and ask questions.
|
|
|
|
```bash
|
|
curl -X POST http://localhost:37777/api/corpus/hooks-expertise/query \
|
|
-H "Content-Type: application/json" \
|
|
-d '{ "question": "What are the 5 lifecycle hooks?" }'
|
|
```
|
|
|
|
Each follow-up question adds to the conversation naturally. If the session expires, the agent auto-reprimes from the corpus file and retries.
|
|
|
|
---
|
|
|
|
## Filter Options
|
|
|
|
Use these parameters when building a corpus to control which observations are included:
|
|
|
|
| Parameter | Type | Description |
|
|
|-----------|------|-------------|
|
|
| `name` | string | Name for the corpus (used in all subsequent API calls) |
|
|
| `project` | string | Filter by project name |
|
|
| `types` | string[] | Filter by observation type (bugfix, feature, decision, discovery, refactor, change) |
|
|
| `concepts` | string[] | Filter by tagged concepts |
|
|
| `files` | string[] | Filter by files read or modified |
|
|
| `query` | string | Full-text search query |
|
|
| `dateStart` | string | Start date filter (YYYY-MM-DD) |
|
|
| `dateEnd` | string | End date filter (YYYY-MM-DD) |
|
|
| `limit` | number | Maximum observations to include |
|
|
|
|
---
|
|
|
|
## Architecture
|
|
|
|
```
|
|
MCP Tools HTTP API
|
|
(mcp-server.ts) (worker on :37777)
|
|
| |
|
|
build_corpus ──┤ |
|
|
list_corpora ──┤ |
|
|
prime_corpus ──┤── callWorkerAPIPost() ──>|
|
|
query_corpus ──┤ |
|
|
rebuild_corpus ──┤ |
|
|
reprime_corpus ──┘ |
|
|
v
|
|
CorpusRoutes
|
|
(8 endpoints)
|
|
/ | \
|
|
CorpusBuilder | KnowledgeAgent
|
|
| | |
|
|
SearchOrchestrator | Agent SDK V1
|
|
SessionStore | query() + resume
|
|
|
|
|
CorpusStore
|
|
(~/.claude-mem/corpora/)
|
|
```
|
|
|
|
**Key insight:** The Agent SDK's `resume` option lets you prime a session once (upload the corpus), save the `session_id`, and resume it for every future question. The corpus stays in context permanently -- no re-uploading, no prompt caching tricks. The 1M token context window makes this viable: 2,000 observations at ~300 tokens each fits comfortably.
|
|
|
|
---
|
|
|
|
## When to Use `/knowledge-agent` vs `/mem-search`
|
|
|
|
| | `/mem-search` | `/knowledge-agent` |
|
|
|---|---|---|
|
|
| **Returns** | Raw observation records | Synthesized conversational answers |
|
|
| **Best for** | Finding specific observations, IDs, timelines | Asking questions about patterns, decisions, architecture |
|
|
| **Token model** | Pay-per-query (3-layer progressive disclosure) | Pay-once at prime time, then cheap follow-ups |
|
|
| **Interaction** | Search, filter, fetch | Ask questions in natural language |
|
|
| **Data freshness** | Always current (queries database live) | Snapshot at build time (rebuild to refresh) |
|
|
| **Setup** | None -- works immediately | Build + prime required before first query |
|
|
|
|
**Rule of thumb:** Use `/mem-search` when you need to find something specific. Use `/knowledge-agent` when you want to understand something broadly.
|
|
|
|
---
|
|
|
|
## API Reference
|
|
|
|
| Method | Path | Description |
|
|
|--------|------|-------------|
|
|
| POST | `/api/corpus` | Build a new corpus from filters |
|
|
| GET | `/api/corpus` | List all corpora with stats |
|
|
| GET | `/api/corpus/:name` | Get corpus metadata |
|
|
| DELETE | `/api/corpus/:name` | Delete a corpus |
|
|
| POST | `/api/corpus/:name/rebuild` | Rebuild from stored filters |
|
|
| POST | `/api/corpus/:name/prime` | Create AI session with corpus loaded |
|
|
| POST | `/api/corpus/:name/query` | Ask the knowledge agent a question |
|
|
| POST | `/api/corpus/:name/reprime` | Fresh session (wipe prior Q&A) |
|
|
|
|
---
|
|
|
|
## Edge Cases
|
|
|
|
- **Session expiry**: If `resume` fails, the agent auto-reprimes from the corpus file and retries
|
|
- **SDK process exit**: If the Claude process exits after yielding all messages, the agent treats it as success when the session_id or answer was already captured
|
|
- **Empty corpus**: A corpus with 0 observations is valid (just empty)
|
|
- **Model from settings**: Reads `CLAUDE_MEM_MODEL` from user settings -- no hardcoded model IDs
|
|
|
|
## Next Steps
|
|
|
|
- [Memory Search](/usage/search-tools) - The 3-layer search workflow for finding specific observations
|
|
- [Progressive Disclosure](/progressive-disclosure) - Philosophy behind token-efficient retrieval
|
|
- [Architecture Overview](/architecture/overview) - System components
|