Skip to content

apm compile

Compile your instructions primitives into the AGENTS.md / CLAUDE.md / GEMINI.md root context files (and per-harness rules trees) that each agent harness reads at startup.

Terminal window
apm compile [OPTIONS]

apm compile reads instructions/*.instructions.md from .apm/ (your project) and apm_modules/ (installed dependencies), then writes one set of root context files plus per-harness rules per resolved target.

For a Claude-only target, compile writes always-on instructions to the root AGENTS.md and each single-directory instruction (for example src/test/**) to exactly src/test/AGENTS.md, plus a CLAUDE.md next to each generated AGENTS.md that imports it with @AGENTS.md. With --single-agents, directory instructions are left out of the root file with a warning. CLAUDE.md carries no instructions itself. Path-scoped instructions (for example **/*.py) stay in .claude/rules/, deployed by apm install; a Claude-only compile omits them from AGENTS.md. A hand-authored CLAUDE.md is never overwritten; if it does not import @AGENTS.md, compile warns, because Claude Code does not load AGENTS.md while such a CLAUDE.md exists. See Claude Code.

The root CLAUDE.md also imports installed packages’ root CLAUDE.md files using manifest and lockfile paths, including transitive packages, ADO org/project/repo layouts, and deeper repository or subdirectory paths. Nested documentation is not imported as a separate package. Without manifest or lockfile metadata, only the legacy owner/package layout is discovered; run apm install to record deeper package roots.

Before writing, compile applies the org policy’s dependency rules to the installed packages recorded in apm.lock.yaml, as apm install does. When a recorded package is no longer allowed and the policy enforces block, compile writes nothing and exits 1 naming the package; remove or replace it in apm.yml and run apm install. Under enforcement: warn it warns and compiles. APM_POLICY_DISABLE=1 skips the check.

Discovery and output are bounded to the active Git checkout. Nested Git repositories and linked worktrees below it are excluded, including when includes: auto is set. Run apm compile from a nested checkout to compile it.

Compile only handles instructions (and optionally a single chatmode to prepend via --chatmode). Other primitive types — prompts, skills, agents, hooks, commands, MCP — are deployed by apm install directly into the harness directories that consume them and are not touched by apm compile’s own deployment step. See Primitives and targets for the full reach map.

After a successful non-dry-run compile, APM also reconciles deployed-file ownership with the current targets: declaration. If the declared target set contracts, artifacts and lockfile entries owned by the removed target are cleaned up with the same hash and user-edit safeguards as apm install — this also includes merge-hook config/sidecar entries (see Hooks and commands for what those files are) even though compile does not itself deploy hooks. Because this step can rewrite apm.lock.yaml, a non-dry-run compile refuses a lockfile written by a newer APM before it changes any file.

When you actually need it: compile is optional for the copilot target — GitHub Copilot natively reads .github/instructions/*.instructions.md (with their applyTo: frontmatter) that apm install already deploys. Compile is recommended for every other context-producing target (claude, cursor, codex, gemini, grok-build, opencode, antigravity, windsurf, kiro, hermes, intellij), which load instructions through a root context file or harness-specific rules folder that compile generates.

Resolution order for which targets to compile:

  1. --target / --all on the command line
  2. targets: field in apm.yml
  3. Auto-detection from the documented filesystem signals

Use apm targets to preview what auto-detection resolves to before compiling.

Before it reads sources, apm compile fires the pre-compile lifecycle event, so a trusted project script can generate instructions under .apm/ first. It does not fire with --dry-run, --validate, --watch, --root, or --global.

The compiled output is scanned for hidden Unicode before any file is written. Critical findings cause the command to exit non-zero. See Drift and secure by default.

FlagDescription
-t, --target VALUETarget(s) to compile. Comma-separated. The help text derives its values from the same catalog used for validation, including intellij.
--allCompile for all canonical targets. Equivalent to --target all and mutually exclusive with --target. Preferred form.

vscode is a deprecated alias for copilot and prints a warning naming the replacement; agents also selects copilot. --target all also emits a deprecation warning — prefer --all.

Every accepted project target is also accepted by compile. grok-build produces AGENTS.md. Targets without root-context output (agent-skills, grok-bot, and the experimental grok-cloud, openclaw, copilot-app, and copilot-cowork) are successful no-ops that write nothing. An experimental target whose flag is off also prints the apm experimental enable hint. See Output layout per target. antigravity, hermes, and grok-bot are explicit-only, while intellij is MCP-only. None is included in all. For intellij, file primitives use the Copilot profile and produce AGENTS.md; IntelliJ-specific integration remains MCP-only. Use apm install or apm update when you want shared .agents/skills/ output.

FlagDescription
--root DIRRedirect generated artifacts (AGENTS.md and per-target files) under DIR while apm.yml and .apm/ still resolve from the current working directory. Claude dependency memory uses the module store described below. Mirrors pip install --target. DIR is created if missing. Not valid with --watch. Pre-compile lifecycle scripts do not run.
-o, --output PATHOutput file path. Only applies in single-file mode (--single-agents). Default: AGENTS.md.
--single-agentsForce single-file compilation (legacy). Writes one combined file at --output instead of a distributed per-directory target-file tree. Applies to every target that uses distributed placement.
--cleanRemove orphaned AGENTS.md files no longer produced by the current primitive set. Nested Git repositories and linked worktrees below the cleanup root are excluded; to clean one, make that checkout the cleanup root, usually by running apm compile --clean there. For --target claude, also removes a stale APM-generated CLAUDE.md once instructions live in .claude/rules/. Hand-authored CLAUDE.md files (files without the <!-- Generated by APM CLI --> marker) are never deleted.

Claude dependency imports prefer the source checkout’s apm_modules/. If it is absent after apm install --root DIR, compilation uses DIR/apm_modules/ and its lockfile, including transitive packages. Imports resolve relative to the generated file, even when sources and output are in different directories. Keep them on the same filesystem drive. For frozen replay, see install’s deploy location.

Project compilation protects root AGENTS.md, CLAUDE.md, and .github/copilot-instructions.md files, and distributed AGENTS.md and CLAUDE.md outputs in subdirectories, including destinations selected with --root. If an existing file has no exact APM-generated marker line among its first five lines, full-file compilation retains it and prints a warning. Live compiles leave the file unchanged; dry runs report that it would be retained without writing it. Delete or rename that file before compiling if generated output should replace it. For section-scoped AGENTS.md updates, see compilation.agents_md.

If a constitution or dependency imports still require CLAUDE.md, --clean regenerates an APM-generated file instead of removing it. An unmarked, hand-authored root CLAUDE.md remains unchanged.

--clean is a file-deleting operation. Before running it live, pass --dry-run to preview exactly what would be removed without touching the filesystem:

Terminal window
apm compile --clean --dry-run --target claude

The dry-run output shows [dry-run] would remove stale CLAUDE.md -- instructions now live in .claude/rules/ when an APM-generated file would be deleted, or [dry-run] would skip removal of CLAUDE.md: hand-authored file will not be deleted. Delete or rename it manually if duplicate context is unwanted. when a hand-authored file is found. No files are written or deleted during dry-run.

For Copilot, apm compile --target copilot --dry-run also previews removal of a stale .github/copilot-instructions.md when all global instructions are deployed under .github/instructions/. This cleanup does not require --clean: only marked, hash-matched generated files are removed. Files without a valid Build ID, hand-authored files, and symbolic links are retained.

FlagDescription
--chatmode NAMEPrepend the named chatmode to the generated AGENTS.md.
--no-linksSkip markdown link resolution.
--with-constitution / --no-constitutionInclude or omit the Spec Kit memory/constitution.md block at the top. Default: included. When disabled, an existing block is preserved but not regenerated.
--local-onlyIgnore apm_modules/; compile only .apm/ primitives.
--force-instructions / --no-force-instructions (alias: --no-dedup)Include instructions in AGENTS.md and .github/copilot-instructions.md even when .claude/rules/, .github/instructions/, or .agents/rules/ already covers that content (overrides the default deduplication). --no-dedup is a hidden backward-compatible alias for --force-instructions. --no-force-instructions (the default) keeps deduplication on.
--legacy-skill-pathsAccepted for compatibility; skill deployment happens in apm install / apm update, not apm compile.
FlagDescription
--watchRe-run compilation on file changes. See Watch mode.
--validateValidate primitives and exit. No files written.
--dry-runShow placement decisions without writing files or running pre-compile scripts.
-v, --verboseShow source attribution and optimizer analysis.

Global compilation keeps supported user-scope root-context targets in sync with globally installed instruction packages — one command, no per-tool setup.

FlagDescription
-g, --globalCompile user-scope root context files from $APM_HOME/apm_modules (default ~/.apm/apm_modules). Writes targets declared by targets: (or the deprecated singular target:) in $APM_HOME/apm.yml, or every supported user-scope target when neither field is declared. APM_HOME selects the input metadata only; compiled files use each target’s home variable or the operating-system home. Not valid with project-output flags such as --target, --all, --watch, --root, or --output. Exits non-zero if the user-scope modules directory does not exist.

apm compile --global is explicit. apm install -g does not run it; instead, when global instructions land on a root-context-only target, install prints a one-line hint pointing at apm compile -g. Run it manually after adding or removing global packages. Hand-authored files (files that do not carry the APM-generated marker) are never overwritten.

For Codex, a non-blank CODEX_HOME selects $CODEX_HOME/AGENTS.md instead of ~/.codex/AGENTS.md. The value must be an absolute path; a relative value fails before anything is written. Project-scope compile is unaffected.

For Claude, each unconditional instruction is omitted from the compiled root only when its expected native rule matches the install renderer’s output. Missing, unrelated, changed, or unsafe rules leave that instruction in the compiled fallback. This uses the user rules directory, including an absolute CLAUDE_CONFIG_DIR outside your home, not the current project’s rules. Other targets retain their existing output.

The complete Claude instruction selection is checked by the compiled-output security policy before native-rule suppression or cleanup, including dry-run. Critical hidden characters fail compilation without changing the Claude root.

An existing redundant user CLAUDE.md is retained until you explicitly clean it:

Terminal window
apm compile -g --clean --dry-run
apm compile -g --clean

Cleanup requires an unchanged generated root matching the current instructions and full native-rule coverage. Hand-authored, edited, unverifiable, or symlinked roots are retained; review and remove or rename them manually if needed. Partial coverage still produces fallback content rather than deleting the root. Dry-run never writes or deletes files.

Because --target is rejected alongside --global, target: or targets: in $APM_HOME/apm.yml is how you narrow user-scope output. When it declares a target set, apm compile -g writes only those targets. If you install with an explicit apm install -g --target, update the manifest declaration before compiling; the explicit install flag does not replace it. Declare nothing and every applicable user-scope root context file is written. The manifest must be a regular, non-symlink file. Malformed or unreadable YAML, a non-mapping document, or an invalid target declaration stops compilation before any target output is written; fix the reported manifest problem and rerun the command.

# $APM_HOME/apm.yml
targets: [claude, codex]
Terminal window
apm compile -g --dry-run

For OpenCode, apm compile -g writes ~/.config/opencode/AGENTS.md and retains scoped applyTo sections.

Terminal window
apm compile -g

apm compile generates root context files: AGENTS.md, CLAUDE.md, GEMINI.md, .github/copilot-instructions.md (see Output layout per target below for the full per-target breakdown). Teams that commit these files into source control face a consistency problem: without target: or targets: set in apm.yml, auto-detection decides which files to produce based on which tool folders exist on the current machine. A contributor with only .claude/ locally produces claude output only; a developer who also has .github/ triggers the two-or-more-folders rule and gets the full all expansion — producing AGENTS.md, CLAUDE.md, GEMINI.md, and .github/copilot-instructions.md. The committed set silently tracks whoever last ran apm compile.

Set targets: in apm.yml to declare exactly which agent formats the project supports. Every run of apm compile — local developer, CI, cloud agent — then writes the same files regardless of which tool folders exist on that machine:

apm.yml
targets: [claude, cursor] # compile writes exactly these two sets; nothing else

This makes the committed generated files deterministic for humans, cloud agents, and contributors who do not run apm compile locally and rely on the checked-in artifacts.

For the full list of accepted targets: values, see manifest schema — targets.

Compile for whatever the project is set up for:

Terminal window
apm compile

Compile for one target:

Terminal window
apm compile --target claude
apm compile --target copilot
apm compile --target cursor

Compile for several targets in one pass:

Terminal window
apm compile -t claude,copilot
apm compile -t claude,gemini

Compile for every canonical target:

Terminal window
apm compile --all

Validate without writing:

Terminal window
apm compile --validate

Preview placement:

Terminal window
apm compile --dry-run
apm compile -t claude,codex --dry-run -v

Skip dependencies and compile only local primitives:

Terminal window
apm compile --local-only

Remove stale AGENTS.md files after deleting primitives (also removes a stale APM-generated CLAUDE.md once .claude/rules/ owns the instructions):

Terminal window
apm compile --clean
apm compile --clean --target claude
apm compile --clean --target claude --dry-run

Redirect generated files to a scratch directory (sources stay in $PWD):

Terminal window
apm compile --root /tmp/agents-out --target copilot
ls /tmp/agents-out # AGENTS.md / per-target files; the source tree stays clean

apm compile --watch monitors the project for source changes and re-runs compilation automatically.

  • Watched directories (when present): .apm/, .github/instructions/, .github/agents/.
  • Triggers on changes to primitive files (such as *.instructions.md, *.agent.md, SKILL.md) and apm.yml; other .md edits are ignored.
  • Editing apm.yml’s target: / targets: mid-session takes effect on the next file event; no need to restart the watcher. The CLI --target flag, when passed to apm compile --watch, still outranks apm.yml.
  • --clean is ignored in watch mode (a [!] warning is printed at startup). Run apm compile --clean separately between watch sessions to remove orphaned outputs.
  • 1-second debounce to coalesce rapid edits.
  • pre-compile lifecycle scripts do not run; run apm compile once to regenerate their output.
  • Press Ctrl+C to stop.
  • Combine with --dry-run to validate placement on every save without writing.
  • When root files are retained, each recompile reports both the retained count and any other outputs generated (or planned in dry-run mode).
Terminal window
apm compile --watch
apm compile --watch --dry-run

Watch mode uses the same target resolution and compilation strategy as one-shot apm compile; --output only applies in single-file mode.

apm compile writes root context files only; apm install manages prompts, agents, commands, hooks, MCP, and skill deployment directories.

TargetCompile outputs
copilotAGENTS.md, .github/copilot-instructions.md
claudeAGENTS.md, CLAUDE.md (imports @AGENTS.md)
grok-buildAGENTS.md
cursorAGENTS.md
codexAGENTS.md
opencodeAGENTS.md
geminiAGENTS.md, GEMINI.md (a stub that imports AGENTS.md; not written, and an APM stub is removed, when there is no AGENTS.md)
antigravityAGENTS.md
windsurfAGENTS.md
kiroAGENTS.md
hermesAGENTS.md
intellijAGENTS.md
agent-skillsnone
grok-botnone
grok-cloud, openclaw, copilot-app, copilot-coworknone
allAGENTS.md, CLAUDE.md, GEMINI.md, .github/copilot-instructions.md

.github/copilot-instructions.md is only managed by APM when its first line is the marker <!-- Generated by APM CLI from .apm/ primitives -->. Catalog root context files such as AGENTS.md, CLAUDE.md, and GEMINI.md are only managed by APM when they carry a generated marker. A hand-authored file is left untouched on both write and cleanup paths. To hand off an existing file to APM, prepend the marker (or delete the file) and re-run apm compile.

There is no --strategy flag. Compilation runs in one of two modes:

  • Distributed (default) — writes a tree of focused target files (e.g. AGENTS.md, CLAUDE.md) next to the code they apply to, plus per-target subdirectories. This is the recommended mode and follows the Minimal Context Principle. Set compilation.strategy: single-file (or compilation.single_file: true) in apm.yml to opt out.
  • Single-file (--single-agents) — writes one combined file at --output (default AGENTS.md). Use when a harness or workflow requires a single context file. With multiple targets, target-specific root content remains separate and is not appended to AGENTS.md.

For a project with two scoped instructions (applyTo: "scripts/**" and applyTo: "tests/**") plus a generic one (no applyTo:), apm compile writes:

AGENTS.md # generic instructions only
scripts/AGENTS.md # instructions scoped to scripts/**
tests/AGENTS.md # instructions scoped to tests/**

Each agent harness then loads only the file nearest the code it is working in. If you did not expect new AGENTS.md / CLAUDE.md files in subdirectories, this is the distributed default in effect; see Where compiled context files land for the rationale, or pass --single-agents for a single-file output.

Placement counts only authored files. AGENTS.md, CLAUDE.md and GEMINI.md files that carry the APM-generated marker header (that is, output from an earlier compile) do not count as applyTo matches, so re-running apm compile on unchanged sources gives the same layout. Hand-authored context files without the marker still count.

When a deployed file that apm.lock.yaml records links into apm_modules/ and the link target is missing (for example after a fresh clone), compile prints one warning naming the file, the missing target and the remedy, apm install. The exit code is unchanged.

CodeMeaning
0Compilation succeeded (or --validate passed).
1No apm.yml, no primitives to compile, validation failure, hidden-Unicode finding, an installed package the org policy no longer allows, or compilation error.
2Conflicting flags (e.g. --all combined with --target, or --root combined with --watch).

Copyright (c) Microsoft Corporation. General documentation: CC-BY-4.0. Software, code examples and OpenAPM: Apache-2.0. See scope and exclusions.