Document from code
There are two ways to use CRAYON-C4. You can design a new system from scratch in the visual editor — open the C4 editor and draw it node by node. Or, if the system already exists in code, you can document it as it evolves: generate a model from the repository, refine it by hand, and re-generate whenever the code moves. This page covers the second path.
The promise that makes this practical: re-running the generator never silently discards your manual curation. Renames, descriptions, grouping, and layout you added by hand survive every update — a three-way merge folds in what changed in the code and keeps what you changed by hand.
The loop
You can drive this from three surfaces: the command line, VS Code, and inside the web app.
Generate from the command line
The generator is a zero-config CLI that understands Next.js, React, .NET, and Java (Maven & Gradle) codebases. From the root of any repository:
npx crayon-c4 .
It scans the project and writes three files:
| File | Purpose |
|---|---|
crayon-c4-project.json | The model file — the C4 diagram data, validated against the schema. This is what you import into a project. |
crayon-c4-provenance.json | A sidecar that records what was generated and when. It's what makes re-runs safe: the merge engine uses it to distinguish your hand-authored elements from generated ones. Keep this alongside the model file. |
crayon-c4-enrichment.json | A task list of the elements whose descriptions the generator couldn't fill from code. The AI enrichment pass (CLI or VS Code) works through this list; you review its edits before they land. |
CLI reference
All flags are optional. A zero-flag run (npx crayon-c4) produces a usable model.
| Flag | Purpose | Default |
|---|---|---|
path | Repository root to scan (positional) | . (current directory) |
--root <path> | Scope the scan to a sub-project; repeat the flag for several roots in a monorepo | whole repository |
--depth <level> | How deep the model goes: context, container, component, or code | component |
--out <file> | Where to write the model file | <path>/crayon-c4-project.json |
--provenance <file> | Where to write the provenance sidecar | <path>/crayon-c4-provenance.json |
--starting <file> | A previous model file to merge into (for re-runs) | none |
--exclude <globs> | Extra exclusion patterns (comma-separated) | engine defaults |
--config <file> | Custom extractor config file | auto-discovered |
--file-cap <n> | Maximum files to analyze | engine default |
--apply | Write merged result (vs report-only) | off |
--dry-run | Report only, write nothing | off |
--json | Machine-readable JSON summary on stdout | human-readable text |
Exit codes: 0 = success, 1 = unexpected error, 2 = usage/config error, 3 = no stack detected, 4 = invalid starting model, 5 = output fails validation.
Project configuration: .crayon/config.json
Settings that should travel with the analyzed repository live in a .crayon/config.json file at its root (an explicit --config path wins; the legacy root-level crayon-extract.config.json is still honoured). The file must carry "schema": 1 and supports:
prompts— project-scoped instructions for the AI passes:appendapplies to every pass,enrichmentandmodelingto their specific pass. These merge before any editor-level prompt setting, so the repo's own conventions always come first.projects— shape the model:excludedrops matched systems/containers (anchored, case-insensitive globs) with all their descendants and touching connections, andbundlesmerges matched containers into one named container. The run report lists everything excluded or bundled — nothing is hidden silently, and surviving element ids never change.flows—entryPointsmarks components (API routes, UI components, controllers) that get a fully traced, method-level F4 flow diagram, with an optional per-entrymaxDepth.static— scan tuning:roots(the config-file equivalent of--root), extraexcludepatterns, and analysis bounds.
The CLI and the VS Code extension share one engine, so the config means the same thing in both.
Generate from VS Code
If you'd rather not leave your editor, the CRAYON-C4 VS Code extension contributes these commands (run from the command palette):
- CRAYON-C4: Generate Model — scan the project and create a model file
- CRAYON-C4: Regenerate — re-run the generator on an existing model file
- CRAYON-C4: Open Architecture Viewer — open the interactive viewer directly on the generated model
- CRAYON-C4: Send to Web App — open the web app's import page in your browser, where you pick the generated model file to merge
- CRAYON-C4: Enrich with AI — fill empty descriptions in the model using AI
- CRAYON-C4: Model Flows & Deployment with AI — generate behavior (F4) and deployment models from code + descriptions
These commands use the same engine as the CLI — it's all one generator, wired into the editor for convenience. The VS Code extension page covers the full workflow: installation, the built-in viewer, the AI passes, settings, and multi-project workspaces.
Create a new project from generated code
Once you have a generated model file, bring it into a CRAYON-C4 project:
- Go to Projects.
- Click New project and choose From code — a file picker opens for the generated
crayon-c4-project.json. - Optionally include the
crayon-c4-provenance.jsonsidecar in the same pick (recommended) so future merges know which parts were generated. - A new project is created with the generated model.
From code is a Pro tier feature. From file (importing any exported project .json) is available on every tier.
Keep it in sync: Update from code
When the code changes, generate again, then sync the project:
- Go to Projects and open the project's detail page (works for local and cloud projects alike).
- Click Update from code, pick the newly generated model file, and optionally include the provenance sidecar.
- A review opens showing every add, change, and removal. Conflicts (where the code renamed something and you renamed it too) are flagged for you to pick.
- Apply the review to merge the changes into your project.
Your hand edits are preserved: code-derived updates land, your curated names, descriptions, grouping, and layout stay, and removals are opt-in so hand-authored elements are never silently dropped.
Hand-author a model instead of generating
If you prefer to write your own model file instead of generating from code, you can. The published CRAYON-C4 JSON Schema plus an authoring guide let you paste a system description into any AI (or write by hand) to produce a valid model. Then import it as a new project, the same way you would a generated file.
Where next
- Projects & sharing — save, share, and export the model you've built.
- The three views of a system — structure (C4), where it runs (deployment), and how work flows (F4).
- Open the editor — or start a design from scratch instead.