ctx generate
ctx generate builds a first knowledge base out of the code itself: it runs the external Graphify CLI to extract a code graph, turns the graph’s subsystems and most-connected components into a pick-list, and stages your selection for your agent to verify and write up.
When you’d reach for it
Section titled “When you’d reach for it”Reach for it when ctx migrate comes back with No existing knowledge found to migrate. — a service with real code and no prose. It gives an agent a map to start from: which subsystems exist, which symbols everything hangs off, and where in the tree each one lives.
Two caveats worth internalising before you run it. First, ctx generate shells out to a separate tool you must install yourself: uv tool install graphifyy (pipx or pip work too). Nothing is vendored, and there’s no fallback path — no graphify on PATH, no candidates. Second, what it stages is a scaffold, not documentation. Every slice file opens with a header telling the agent the content is auto-extracted, that INFERRED edges are guesses, and that it must read the cited source before writing anything. Treat the output as a reading list, not as pages.
Synopsis
Section titled “Synopsis”ctx generate # analyze this project, pick, stagectx generate --list # run the analysis and print candidates onlyctx generate --type entity # narrow to componentsctx generate --all --yes # stage everything, no promptsctx generate --from ../other-repo --backend gemini| Flag | Type | Default | Description |
|---|---|---|---|
--all | bool | false | select every item without prompting |
--backend | string | optional Graphify LLM backend for richer subsystem names (gemini|claude|ollama|...) | |
--from | string | codebase directory to analyze (default: this project) | |
--list | bool | false | only list what would be generated; stage nothing |
--type | string | only items of this suggested type (concept|entity|analysis) | |
--yes, -y | bool | false | skip the confirmation prompt |
--backend is handed only to Graphify’s clustering pass (graphify cluster-only … --backend=<name>), where it buys you human-sounding subsystem names instead of hub-derived ones. It is not passed to the extraction pass, so it cannot fix an extraction that fails for want of an API key — see Common errors. Clustering is best-effort anyway: if it fails, the run continues with the graph extraction already produced.
--from is a directory only. Unlike ctx migrate, it does not accept a git URL and does not clone anything.
--list skips the staging, not the work. Graphify still runs end to end and the slice files are still written into .contexo/.generate-cache/; the only thing --list suppresses is the manifest. Budget for the full analysis time whenever you invoke it.
--type accepts concept (a subsystem), entity (a component) or analysis (the codebase overview), and renumbers the list — the IDs at the prompt always refer to the list you’re looking at. --all answers the selection prompt but not the confirmation, so scripts need --all --yes together.
| Flag | Type | Default | Description |
|---|---|---|---|
--root | string | project root directory (default: current directory) |
Examples
Section titled “Examples”Analyze the current project and look at what you’d get:
ctx generate --listError: graphify extract failed: exit status 1: error: no LLM API key found (3 doc/paper/image file(s) need semantic extraction). Set GEMINI_API_KEY or GOOGLE_API_KEY (gemini), MOONSHOT_API_KEY (kimi), ANTHROPIC_API_KEY (claude), OPENAI_API_KEY (openai), DEEPSEEK_API_KEY (deepseek), or pass --backend. A code-only corpus needs no key. Or pass --code-only to index just the code (local AST, no key) and skip the non-code files.
[graphify extract] scanning ~/projects/acme-api
[graphify extract] found 4 code, 3 docs, 0 papers, 0 images
[graphify extract] 1 file(s) not classified (no supported extension or shebang), skipped: .gitignoreThat capture is the failure mode you’re most likely to hit first, and it’s worth reading closely: Graphify’s extract step found non-code files (Markdown, papers, images) that it can only index semantically, so it demands an LLM key. ctx invokes graphify extract <root> with no flags, so the fix is on Graphify’s side — export one of the keys it names (GEMINI_API_KEY, ANTHROPIC_API_KEY, …) before running ctx generate. Passing --backend will not help here.
Stage everything the analysis found, without prompts:
ctx generate --all --yesOn success this prints Staged N item(s). Ask your agent: "finish the contexo generation". Say that to your agent and it works through the staged slices via ctx_generate.
Narrow to the components rather than the subsystems — useful when you want entity pages for the handful of types everything depends on:
ctx generate --type entityHow it works
Section titled “How it works”-
Requires
.contexo/under the project root (--root, else the current directory), andgraphifyonPATH. -
Runs
graphify extract <project-root>, then best-effortgraphify cluster-only <project-root> --no-viz(plus--backend=…if you passed one). Clustering adds community names andGRAPH_REPORT.md. -
Graphify writes into a
graphify-outdirectory inside whatever it analyzed — the project root, or the--fromdirectory.ctxthen moves that directory to.contexo/.generate-cache/graphify-outso the analyzed tree is left clean. If the move fails — a different volume, a file still open — it falls back to readinggraphify-outin place, and that directory stays where Graphify put it for you to delete. -
Parses
graphify-out/graph.json(networkx node-link format) and maps it to candidates, in this order:- the
GRAPH_REPORT.mdoverview → oneanalysiscandidate, slugcodebase-overview; - up to 15 communities, largest first →
conceptcandidates named fromcommunity_name(orcommunity <id>when clustering didn’t run); - up to 15 “god nodes” — the highest-degree symbol nodes, with file nodes excluded →
entitycandidates.
So a repository of any size yields at most 31 items.
- the
-
For each community and component it writes a Markdown slice under
.contexo/.generate-cache/slices/—community-<slug>-<id>.mdandcomponent-<slug>.md— listing members withpath:linesource references and the typed edges between them, under a header marking the whole thing auto-extracted and unverified. -
If
graph.jsonis missing or unparseable, the run degrades to the overview candidate alone rather than failing. If the report is missing too, there are no candidates and you get the shared catalog renderer’s message,No existing knowledge found to migrate. -
After you pick and confirm,
.contexo/migrate.jsonis written withkind: "generate",source_root,external_cachepointing at.contexo/.generate-cache, and one entry per pick. No pages are written and nothing is sent to the Contexo server. -
ctx_generatethen returns aGENERATE_RESUMEdirective for exactly those items: read each slice and the source files it cites to verify, callctx_write_pagewith a name you chose from the real code (not the community label), write the<YYYY-MM-DD>-generate-<label>provenance page,ctx_pushwithno_distill=true, and finish withctx_generate(done=true)— which clears the manifest and deletes.contexo/.generate-cache.
Common errors
Section titled “Common errors”ctx generate needs Graphify — install it with `uv tool install graphifyy` (or pipx/pip install graphifyy)— nographifyexecutable onPATH. Checked before Graphify is asked to do any work, so nothing has been written yet.graphify extract failed: exit status 1: error: no LLM API key found …— the codebase contains docs/papers/images that Graphify wants to index semantically. Export one of the keys it lists and re-run.--backendapplies to clustering only and won’t satisfy this.generate: no .contexo/ here — run 'ctx init' first— run from the project root or pass--root.generate: index 40 out of range 1..31/generate: bad index "x"/generate: empty selection— the selection line didn’t parse. Use1,2,5-7,allor*; indices are 1-based against the list as rendered after--typefiltering.No existing knowledge found to migrate.(from a generate run) — Graphify produced neither a usablegraph.jsonnor aGRAPH_REPORT.md. The message text is shared withctx migrate; the cause here is an empty or failed analysis.
See also
Section titled “See also”- Generate guide — installing Graphify and the full walkthrough of verifying slices
ctx migrate— the right command when the project already has docsctx_generate— the MCP tool that consumes the staged manifestctx push— where the agent sends the pages it writes
