Skip to content
Français

Use native CLI hooks

Run your code when a CLI starts, calls a tool or finishes.

Attach a callback to your CLI’s lifecycle to inspect or control its actions. Start with a configured agent. The Outpost loop has separate hooks.

This Claude hook refuses Bash calls and asks the agent to use file-reading tools. Pass harness to createAgent({ harness }), then dispatch the agent.

import {
  createClaudeHarness,
  defineClaudeHook,
} from "@elie-laloum/outpost/core";

export const harness = createClaudeHarness({
  authentication: "account",
  hooks: [
    defineClaudeHook({
      on: "PreToolUse",
      matcher: "Bash",
      run() {
        return {
          hookSpecificOutput: {
            hookEventName: "PreToolUse",
            permissionDecision: "deny",
            permissionDecisionReason: "Use file-reading tools for this task.",
          },
        };
      },
    }),
  ],
});

The callback runs on the host. It receives the native event, a borrowed sandbox and an abort signal. Returning nothing leaves the action unchanged; returning a decision applies that CLI’s rules.

Each CLI has its own factory, event names and decisions. The API lists all 85 exposed events and links to their native documentation.

CLIDeclare a hookAvailable events
Claude CodedefineClaudeHookClaudeHookPhase
CodexdefineCodexHookCodexHookPhase
Copilot CLIdefineCopilotHookCopilotHookPhase
Kimi CodedefineKimiHookKimiHookPhase
AntigravitydefineAntigravityHookAntigravityHookPhase
  • Callbacks need Node.js and a sandbox with liveInput. An invalid decision or callback error fails the turn. Honor signal for host work.
  • Use command: { executable: "node", arguments: ["scripts/check-hook.mjs"] } instead of run for an existing script. It receives native JSON on stdin and follows the CLI’s stdout and exit-code rules.
  • Outpost installs and removes its hook declarations around each CLI process. Ordering and matching remain native; existing hooks are preserved.

To share these hooks, declare them in defineAgentProfile({ hooks }). The harness selects its profile section before appending local hooks.

After building the package, run node examples/74-lifecycle-hooks/index.ts claude from the repository. The complete example supports all five CLIs and explains the required account login and Docker image.