[FEATURE] Expose current model as environment variable in spawned subprocesses

Resolved 💬 4 comments Opened Mar 23, 2026 by CognitiveDisson Closed May 2, 2026

Preflight Checklist

  • [x] I have searched existing requests and this feature hasn't been requested yet
  • [x] This is a single feature request (not multiple features)

Problem Statement

Claude Code sets CLAUDECODE=1 in subprocesses it spawns, which lets tools detect they're running inside Claude Code. However, there is no way to determine which model is currently active from within a subprocess.

This makes it impossible to:

  • Tag invocations with the model that triggered them
  • Log or audit which model was used for a given automated action
  • Adapt subprocess behavior based on model capabilities

ANTHROPIC_MODEL is an input variable (read at startup) and is not updated when the user switches models mid-session with /model. So it cannot be relied on.

Proposed Solution

Set a CLAUDECODE_MODEL environment variable in all subprocesses Claude Code spawns, reflecting the currently active model at the time the subprocess is invoked. It should:

  • Be updated when the user switches models with /model
  • Be set to the fully-qualified model ID (e.g. claude-sonnet-4-6, not an alias)
  • Follow the same lifecycle as CLAUDECODE=1

Alternative Solutions

  1. SessionStart hook + temp file: Write the model to a file at session start, read it in subprocesses. Works for the initial model but goes stale after a /model switch mid-session.
  2. ANTHROPIC_MODEL env var: Only reflects what the user set before launching; not updated dynamically. Unreliable if the user doesn't set it or switches models.
  3. UserPromptSubmit hook: Fires per prompt but current hook inputs do not include the active model, and hooks have no mechanism to inject env vars into subsequent

subprocess calls.

  1. Write to settings.json + ConfigChange hook: Confirmed not to work: the active model does not reload within the session (see #30806 comment).

None of these provide a reliable, always-current model value for subprocesses without requiring significant workarounds.

Priority

Medium - Would be very helpful

Feature Category

Interactive mode (TUI)

Use Case Example

A developer has a wrapper script that detects CLAUDECODE=1 and annotates automated tool invocations with metadata (e.g. tags, labels, audit logs). For better observability, they also want to record which model initiated each action. For example, to later correlate output quality or cost with the model used.

Today, this isn’t possible to do reliably. If a session starts with Sonnet and then switches to Opus via /model, any subprocess invoked afterward has no way of knowing that the active model has changed. If Claude Code exposed something like CLAUDECODE_MODEL, the wrapper script could simply read it:

if [ -n "$CLAUDECODE" ]; then
  echo "Invoked by Claude Code (model: ${CLAUDECODE_MODEL:-unknown})"
fi

This would make it straightforward to consistently capture model-level metadata across tool invocations.

Additional Context

Related issues

  • #30806
  • #34340
  • #34709

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗