claude-code - 💡(How to fix) Fix [FEATURE] Sub-agents should inherit CLAUDE_ENV_FILE environment variables from parent session [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#46696Fetched 2026-04-12 13:35:28
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×4commented ×1

When a parent session writes environment variables to CLAUDE_ENV_FILE via a SessionStart hook, sub-agents spawned via the Agent tool do not inherit those variables. Each sub-agent gets its own empty CLAUDE_ENV_FILE (under a different session-env/<session-id>/ path), and SessionStart does not fire for sub-agents.

Root Cause

When a parent session writes environment variables to CLAUDE_ENV_FILE via a SessionStart hook, sub-agents spawned via the Agent tool do not inherit those variables. Each sub-agent gets its own empty CLAUDE_ENV_FILE (under a different session-env/<session-id>/ path), and SessionStart does not fire for sub-agents.

Fix Action

Fix / Workaround

We run an orchestrator agent (claude --agent inbound-orchestrator) that dispatches 5 parallel worker sub-agents via the Agent tool. The orchestrator's SessionStart hook loads .env variables (API keys, SMTP credentials, DB paths) into CLAUDE_ENV_FILE. The orchestrator's own Bash commands can access these variables, but the worker sub-agents cannot — their Bash commands see empty environment.

Current Workaround

Code Example

# In the PreToolUse hook script:
prefix="set -a && . '${env_file}' && set +a && "
jq -n --arg prefix "$prefix" --arg cmd "$cmd" '{
  hookSpecificOutput: {
    hookEventName: "PreToolUse",
    updatedInput: {command: ($prefix + $cmd)}
  }
}'
RAW_BUFFERClick to expand / collapse

Feature Type

New feature request

Description

When a parent session writes environment variables to CLAUDE_ENV_FILE via a SessionStart hook, sub-agents spawned via the Agent tool do not inherit those variables. Each sub-agent gets its own empty CLAUDE_ENV_FILE (under a different session-env/<session-id>/ path), and SessionStart does not fire for sub-agents.

Use Case

We run an orchestrator agent (claude --agent inbound-orchestrator) that dispatches 5 parallel worker sub-agents via the Agent tool. The orchestrator's SessionStart hook loads .env variables (API keys, SMTP credentials, DB paths) into CLAUDE_ENV_FILE. The orchestrator's own Bash commands can access these variables, but the worker sub-agents cannot — their Bash commands see empty environment.

This forces us to work around the limitation by using a PreToolUse hook that prepends set -a && . '.env' && set +a && to every Bash command via updatedInput. This works but adds overhead and visual noise to every command.

Expected Behavior

Sub-agents spawned via the Agent tool should inherit environment variables that the parent session wrote to CLAUDE_ENV_FILE. Either:

  1. Sub-agents should receive a pre-populated CLAUDE_ENV_FILE containing the parent's exports, OR
  2. Sub-agents should fire their own SessionStart hooks (currently only SubagentStart fires, which cannot inject env vars), OR
  3. A new mechanism (e.g., envFile in settings.json that accepts a file path) should propagate .env to all sessions globally

Current Workaround

PreToolUse hook on Bash that sources .env before every command via updatedInput:

# In the PreToolUse hook script:
prefix="set -a && . '${env_file}' && set +a && "
jq -n --arg prefix "$prefix" --arg cmd "$cmd" '{
  hookSpecificOutput: {
    hookEventName: "PreToolUse",
    updatedInput: {command: ($prefix + $cmd)}
  }
}'

Environment

  • macOS, Claude Code latest
  • Using Agent tool to spawn sub-agents from an orchestrator agent
  • SessionStart hook writes to CLAUDE_ENV_FILE (documented pattern)

Impact

High - Blocks reliable use of environment variables in multi-agent pipelines. Any project using Agent tool + API keys/credentials is affected.

extent analysis

TL;DR

Implement a mechanism to propagate environment variables from the parent session to sub-agents, such as modifying the SessionStart hook or introducing a new settings option.

Guidance

  • Investigate modifying the SessionStart hook to write environment variables to a shared location accessible by sub-agents.
  • Consider introducing a new settings option, such as envFile in settings.json, to globally propagate .env files to all sessions.
  • Explore alternative approaches to sourcing environment variables in sub-agents, such as using a centralized environment variable storage.
  • Verify that any proposed solution does not introduce security risks or conflicts with existing environment variable management.

Example

# Example of a potential new settings option in settings.json
{
  "envFile": "/path/to/shared/env/file"
}

Notes

The current workaround using a PreToolUse hook adds overhead and visual noise, and a more robust solution is needed to reliably propagate environment variables to sub-agents.

Recommendation

Apply a workaround, such as modifying the SessionStart hook or introducing a new settings option, to propagate environment variables from the parent session to sub-agents, as upgrading to a fixed version is not currently an option.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING