KnightCode Docs
Reference

Environment Variables

KnightCode uses environment variables in three ways:


KnightCode uses environment variables in three ways:

  • Variables such as KNIGHTCODE_OFFLINE configure the KnightCode process.
  • KnightCode sets process markers so child processes can identify KnightCode as the launching agent.
  • Commands run by the LLM-callable shell tools receive KNIGHTCODE_* variables describing the current session.

Provider API-key variables are documented separately in Providers.

Process Marker

The CLI and RPC entry points set two process markers:

  • AI_AGENT=knightcode is a generic marker that lets tooling identify KnightCode as the agent that launched the process.
  • KNIGHTCODE_CODING_AGENT=true is KnightCode-specific and lets child processes detect that they run inside KnightCode.

Child processes inherit both markers. They are not session-specific and are not set automatically when KnightCode is embedded through the SDK.

Shell Tool Session Environment

Commands run by the bash and powershell tools receive the current KnightCode session state:

VariableDescription
KNIGHTCODE_SESSION_IDCurrent session ID
KNIGHTCODE_SESSION_FILEAbsolute path to the current session JSONL file; unset for ephemeral sessions
KNIGHTCODE_PROVIDERCurrently selected model provider
KNIGHTCODE_MODELCurrently selected model ID
KNIGHTCODE_REASONING_LEVELCurrent effective reasoning level: off, minimal, low, medium, high, xhigh, or max

The values are resolved when each command starts. Switching models or changing the reasoning level therefore affects the next shell command without restarting KnightCode. KNIGHTCODE_PROVIDER and KNIGHTCODE_MODEL identify the selected KnightCode model, not a different upstream model that a router may choose internally.

When asked which model or provider is running, inspect these variables instead of inferring the answer from the system prompt:

printf '%s/%s\n' "$KNIGHTCODE_PROVIDER" "$KNIGHTCODE_MODEL"
printf 'reasoning=%s session=%s\n' "$KNIGHTCODE_REASONING_LEVEL" "$KNIGHTCODE_SESSION_ID"

The session file can be inspected directly when the session is persistent:

if [ -n "$KNIGHTCODE_SESSION_FILE" ]; then
  tail -n 1 "$KNIGHTCODE_SESSION_FILE"
fi

These variables are injected into the LLM-callable bash and powershell tools. They are not injected into user-entered ! or !! commands.

Custom Shell Tools

Tools created with createBashTool() or createPowerShellTool() expose the session environment by default when registered with KnightCode. Injection happens before spawnHook, so a hook receives the variables in ctx.env:

const bashTool = createBashTool(cwd, {
  spawnHook: (ctx) => ({
    ...ctx,
    env: { ...ctx.env, CI: "1" },
  }),
});

Disable session metadata independently of the spawn hook:

const powershellTool = createPowerShellTool(cwd, {
  exposeSessionEnvironment: false,
  spawnHook: (ctx) => ctx,
});

When disabled, KnightCode removes inherited values for these variables so nested KnightCode processes do not expose stale parent-session metadata.

KnightCode Process Configuration

These variables are read by KnightCode itself:

VariableDescription
KNIGHTCODE_CODING_AGENT_DIROverride the config directory; default is ~/.knightcode/agent
KNIGHTCODE_CODING_AGENT_SESSION_DIROverride session storage; overridden by --session-dir
KNIGHTCODE_PACKAGE_DIROverride the package directory, useful for Nix/Guix store paths
KNIGHTCODE_OFFLINEDisable automatic network activity, including model catalog refreshes
KNIGHTCODE_DISABLE_FILE_CHECKPOINTSSet to 1, true, or yes to stop backing up files before edits; /undo then rewinds the conversation only. See Sessions
KNIGHTCODE_SKIP_VERSION_CHECKDisable automatic version checks and downloads; explicit knightcode update still works
KNIGHTCODE_DISABLE_AUTO_UPDATESet to 1 to disable automatic update downloads while retaining update notices, overriding the user-level autoUpdate setting; explicit knightcode update still works
KNIGHTCODE_TELEMETRYOverride install/update telemetry and provider attribution headers: 1/true/yes or 0/false/no
KNIGHTCODE_CODEMODE_MAX_WORKERSMaximum codemode workers alive at once across all sessions in the process; default 2. Each worker's script may use up to 256 MB. Raise it when one process runs several sessions, such as in-process subagents. Further scripts wait for a free worker. See Codemode
KNIGHTCODE_CACHE_RETENTIONSet to long for extended provider prompt caching where supported
KNIGHTCODE_SHARE_VIEWER_URLOverride the base URL used by /share
KNIGHTCODE_BUG_ENDPOINTOverride the origin /bug uploads go to; default is https://remote.knightcode.dev
KNIGHTCODE_CLEAR_ON_SHRINKSet to 1 to clear empty rows when rendered content shrinks; the terminal.clearOnShrink setting takes precedence. See Settings
KNIGHTCODE_HARDWARE_CURSORSet to 1 to show the hardware cursor; see Terminal setup
KNIGHTCODE_CLIPBOARD_OSC52Set to 1 to also send copied text to the terminal with OSC 52 after the local clipboard write, as KnightCode does over SSH. Use it when a remote client reaches KnightCode without SSH variables, such as a multiplexer attached from another machine or a remote IDE daemon
KNIGHTCODE_HYPERLINKSOverride OSC 8 hyperlink detection with 1, 0, or auto
KNIGHTCODE_PROGRAM_STATUSOverride OSC 7501 program status detection: 1 always reports, 0 never reports; otherwise KnightCode reports only after the terminal confirms support. See Terminal setup
KNIGHTCODE_IMAGE_PROTOCOLOverride inline image detection with kitty, iterm2, none, or auto
KNIGHTCODE_TRUE_COLOROverride truecolor detection with 1, 0, or auto
KNIGHTCODE_TUI_ESC_TIMEOUTHow long to wait after a lone ESC before treating it as Escape, in milliseconds; defaults to 100 over SSH and 10 otherwise. Increase if Alt-key input is misread as Escape
BRAVE_API_KEYBrave Search key for websearch when none is stored with /tools; see Web tools
VISUAL, EDITORExternal editor fallback when externalEditor is unset
HTTP_PROXY, HTTPS_PROXYProxy outbound HTTP requests

Update controls

Automatic updates are on by default. For a persistent preference without environment variables, open /settings and set Automatic updates to false, or set "autoUpdate": false in your user-level settings.json; see Disable automatic updates for file locations and an example.

To use an environment variable instead, set it before launching KnightCode. For the current PowerShell terminal:

$env:KNIGHTCODE_DISABLE_AUTO_UPDATE = "1"
knightcode

To persist it for your Windows user:

[Environment]::SetEnvironmentVariable("KNIGHTCODE_DISABLE_AUTO_UPDATE", "1", "User")

Then restart your terminal app or IDE so new KnightCode processes inherit it. On macOS or Linux, add this line to your shell profile (~/.zshrc or ~/.bashrc), then open a new terminal:

export KNIGHTCODE_DISABLE_AUTO_UPDATE=1

This variable overrides autoUpdate: true. Any non-empty value disables automatic downloads, including 0; unset it to return control to settings.json. To suppress update notices and checks too, use KNIGHTCODE_SKIP_VERSION_CHECK=1 instead. Explicit knightcode update remains available with either variable.

Provider credentials such as ANTHROPIC_API_KEY, OPENAI_API_KEY, and provider-specific configuration are listed in Providers.