Environment Variable Scoping Across Agent Runs
Agents keep credentials after they forget why, so scope them explicitly.

Environment variable scoping across agent runs depends on execution boundaries. Safe, predictable runs require explicit control over what enters the process, what the environment isolates, what compaction preserves, and what each subagent inherits.
Why environment variables don't automatically re-scope between agent runs
When a session exhausts its context budget, the agent can lose its memory of earlier work while retaining access to the credentials used to perform it. The new context window truncates what the agent knows while leaving what its host process can read unchanged. That mismatch allows authority granted for earlier work to remain available after the reasoning behind it has disappeared.
As a structural default, most harnesses pass the host environment through unless an operator restricts it. Credentials that are there at startup stay available throughout the run.
How shell inheritance works as the default credential path into every agent session
The credential path begins when the harness starts the agent as a child process of the host shell. The inherited environment can include API tokens, cloud CLI sessions, and database connection strings, all accessible from the first instruction. Once a raw value enters that environment, code the agent executes can read it. This includes code produced in response to a malicious prompt, making shell inheritance an entry point for credential exposure.
Codex Cloud illustrates the distinction between giving a process a credential and allowing it to use one. Ordinary environment variables give programs the values themselves, while network secrets give them a placeholder. A proxy substitutes the real value only for requests to an allowed domain over HTTPS on port 443. The raw credential therefore never lands in a local process or file.
Cursor's May 13, 2026 changelog introduced build secrets support for credentials used during a Dockerfile build step, such as pulling private packages. Those credentials are scoped to the build step and are not passed to the running agent environment. The distinction separates authority needed to prepare an environment from authority needed to execute a task.
Where the boundaries actually sit: process, VM, context window, and subagent

A useful diagram would place four layers from top to bottom, namely process, VM, context window, and subagent. The process boundary determines what the shell passes in, while the VM boundary determines what execution state is isolated. The context-window boundary governs what remains in conversational memory, and the subagent boundary governs what a delegated worker inherits. These boundaries overlap, but none substitutes for the others.
At the VM layer, an isolated run has its own filesystem and process table. Credentials injected during environment setup can nevertheless remain present for the VM's full lifetime. Codex Cloud and Cursor's May 2026 update keep secrets scoped to each environment, so its credentials stay out of other environments.
Environment state can also outlive the session that prepared it. Anthropic runs each Claude Code cloud session in a fresh Ubuntu VM, but caches the setup script's resulting filesystem snapshot for roughly seven days for reuse in the same environment. At the same time, compaction can keep earlier credential references in a summary without changing what the process can access. A spawned subagent introduces a further handoff, where optional settings such as sandbox mode inherit from the parent unless explicitly set.
How Claude Code, Codex, and Cursor handle scoping differently by default
Claude Code combines several distinct controls. Its Seatbelt/bubblewrap+socat sandbox with a local proxy is off by default, leaving shell inheritance unrestricted unless sandboxing is enabled. Its MEMORY.md store, which the machine writes on its own, can keep content that referred to credentials even after a session resets. Cloud tasks run in fresh Ubuntu 24.04 VMs, and their environment variables and network access are set when the environment is configured. The local sandbox default and the cloud VM boundary describe different surfaces of isolation.
Codex uses Seatbelt/bwrap+seccomp enforcement, with network access off by default in both read-only and workspace-write modes. But its [shell_environment_policy] block inherits all variables by default, including KEY/SECRET/TOKEN-style names unless ignore_default_excludes is explicitly set to false. Codex v0.162.0, released October 8, 2026, provides the configuration reference for that behavior. Its context memory is repository-state-based, and it does not autonomously mutate its AGENTS.md instruction chain. After a hard reset, historical recall is absent unless the opt-in Memories feature is enabled, while process-level access remains unaffected.
Cursor runs in the editor with the user's full environment, including shell credentials, the SSH agent, and live cloud CLI sessions. Its sandbox uses Seatbelt/Landlock+seccomp with deny-by-default network access, and Linux enforcement requires kernel 6.2+ with Landlock v3. Its May 13, 2026 update added environment-scoped secrets and build-step scoping. Environment version history also supports rollback, making earlier configurations recoverable, including states that held over-privileged credentials.
What context compaction does to credential references that survived earlier in the session
The context-window boundary is less intuitive because compaction changes the agent's account of its work while leaving its execution rights unchanged. If an early instruction names a credential and that instruction enters the summary, the next context window can continue acting on it. The surrounding task history may be gone, but the summarized authorization decision remains. This exposure can exist even when VM and process isolation are already configured.
Compaction mechanisms differ in what they retain and how they present it. Codex CLI uses a single-layer handoff summary. Claude Code works through three tiers in turn, using tool result trimming, Prompt Cache-friendly strategies, and a structured LLM summary with 9 defined sections. OpenCode pairs a marker-based compaction boundary with a summary, leaving hidden messages intact but deprioritized. These mechanisms differ, but none makes omission of credential references automatic.
The mitigation is explicit exclusion before the summary is written. Summary templates or compaction configurations should strip credential references. This addresses the remembered instruction while leaving the underlying credential grant unchanged.
How subagent spawning propagates scoping decisions down the task graph
Unless overridden per subagent, children inherit the parent's sandbox mode, network access, and credential environment. A permissive parent therefore grants more than local authority. Its omissions propagate through every task it delegates, including tasks that need only part of its access.
Claude Code 2.1.296, shipped October 9, 2026, added CLAUDE_CODE_WORKFLOW_SUBAGENT_MODEL to make every workflow agent use one chosen model while other subagents keep their own.
The appropriate pattern resembles an API gateway's treatment of each downstream service call, with the minimum credential set for the specific task explicitly declared. Each subagent therefore needs its own stated sandbox and credential policy.
How environment-scoped secrets and proxy substitution change the risk model
Environment scoping binds credentials to a particular execution environment. Proxy substitution keeps the raw value out of code that the agent runs while letting it make authorized network requests. Together they provide defense in depth, limiting both which environment receives authority and where the credential itself can be read.
Ellipsis runs Claude Code and Codex in cloud sandboxes with environments defined in YAML. Each definition references centrally stored secrets by name, and setup-script results are saved for reuse. The credential boundary is therefore the environment definition. Codex Cloud's Personal vault model adds a user boundary within shared environments. An environment can declare a required key while each team member supplies it from their own account.
Network policy adds another constraint to proxy substitution. Codex Cloud applies both the environment's internet access settings and workspace-level requirements set by Enterprise admins in Agent Security. A restriction at either layer applies regardless of permission at the other, and there is also an explicit escape hatch, since approved full sandbox escalation, where policy permits it, can bypass the command proxy.
Configuring a cloud agent environment for correct scoping from the start
The configuration sequence begins before publication by mapping each credential to the narrowest environment that needs it. Credentials for the payments service belong in the payments environment definition, while credentials for the documentation build belong in the documentation environment. Access from one environment to another should need an explicit grant. YAML or environment configuration files make this mapping reviewable in the same pull request workflow used for code.
The second pre-publication decision is how credentials reach external services. Choose network secrets over environment variables where proxy substitution is available for that use. For a credential that needs only one domain, the proxy allowlist should be as narrow as that use case. Raw-value access by agent-executed code is set in environment design, so this decision belongs there.
At task configuration time, the third step is to declare each subagent's sandbox mode and credential set. Treat that declaration as a service account review, granting only the access the delegated task needs. The fourth step is to configure compaction to exclude credential references from summaries. Check that the summary template or compaction configuration leaves those references out. After a context reset, audit the agent's first actions for re-authentication using a credential it retained through compaction.
The fifth step is to treat the published environment as a versioned artifact whose credential state changes deliberately. Codex Cloud's publish/republish model and Cursor's version history with rollback make that lifecycle explicit. A credential rotation without republication leaves new tasks starting from a stale credential set. Republishing after a dependency or credential change should be as routine as merging a dependency update PR.
Why running agents in isolated VMs is a prerequisite, not a luxury, for this scoping model to work
These controls depend on an execution boundary that the environment definition cannot create by itself. When agents share a machine, process-level access can allow one agent's credentials to bleed into another's execution context. Environment scoping is a configuration layer over isolation. A declaration that credentials belong to environment A is insufficient if environment B can reach the state holding them.
A separate VM for each task supplies the filesystem, process table, and network namespace that make environment-level separation enforceable. The practical test is whether an agent can install packages, start services, drive a browser, and verify its work without sharing compute with another session. If it cannot, credential isolation is partial at best.
Cleanly bounded runs also make attribution meaningful. To track the source, harness, model, and credential for every agent minute, analytics need to know where one run ends and another begins. Shared state leaves that attribution boundary undefined. Isolated VMs provide the foundation, while explicit process, context, and subagent policies determine what authority crosses each boundary within it.


