Architecture
How the CLI, agent loop, file tools, and schema fit together.
The schema
schema.md is the LLM's behavioral spec. It defines directory structure, page format, YAML frontmatter conventions, cross-linking rules, naming conventions, and step-by-step workflows for ingest, query, and lint. The agent reads it at the start of every operation.
Editing schema.md is the primary way to customize behavior — no code changes needed.
The agent loop
Every command that calls the LLM (ingest, query, lint) runs the same agentic loop. The loop sends a prompt to the model, receives a response, executes any tool calls the model makes, feeds results back, and repeats — up to 40 turns — until the model signals it is done.
The model is given five file tools:
| Tool | Purpose |
|---|---|
read_file(path) | Read any file relative to the wiki root. |
write_file(path, content) | Write or overwrite a file. Creates parent directories as needed. |
append_to_file(path, content) | Append content to a file. Used for log.md. |
list_directory(path) | List files and subdirectories at a path. |
search_wiki(query) | Full-text search across all wiki/*.md files. |
All paths are validated against the wiki root to prevent directory traversal.
Package structure
wiki_builder/
├── cli.py # Typer CLI — init, ingest, query, lint, status, config
├── agent.py # Agent loop, Anthropic + OpenAI backends, prompt builders
├── tools.py # The five file tools the LLM calls
├── config.py # Provider and API key resolution
└── templates.py # Default schema.md, index.md, log.md written by init
Ingest workflow
When you run wiki ingest raw/paper.md, the agent:
- Reads
schema.mdand the source file content. - Lists
wiki/sources/,wiki/concepts/, andwiki/entities/to understand what already exists. - Writes a summary page to
wiki/sources/<slug>.md. - Creates or updates concept pages in
wiki/concepts/. - Creates or updates entity pages in
wiki/entities/. - Updates
wiki/index.mdwith new entries. - Appends an entry to
wiki/log.md.
A typical ingest touches 5–15 files across 10–20 tool-calling turns.
Query workflow
When you run wiki query "...", the agent:
- Reads
schema.mdandwiki/index.md. - Identifies relevant pages based on the question and the index.
- Reads those pages in full.
- Synthesizes an answer with citations to source pages.
- If
--saveis set, writes the answer towiki/concepts/and appends tolog.md.
Lint workflow
When you run wiki lint, the agent:
- Reads
schema.mdand lists all wiki pages. - Reads pages to check for orphans, broken links, contradictions, and stale claims.
- Produces a structured lint report.
- If
--fixis set, rewrites pages to repair issues and appends tolog.md.