Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:416rehman DeepZero Backend Env Sanitization

From Leeroopedia
Knowledge Sources
Domains Optimization, LLMs, Security
Last Updated 2026-10-27 00:00 GMT

Overview

Strip metered API keys from the child process environment when invoking a CLI-agent backend, so the CLI uses its own subscription login rather than burning a metered key that happens to be in `.env` for another backend.

Description

When a user configures a CLI-agent backend like ClaudeCodeBackend, they signal intent to use that agent's own subscription auth (OAuth/keychain). But the user's `.env` file may also contain `ANTHROPIC_API_KEY` for LiteLLM-based runs. If that key leaks into the child process, the CLI may use the metered API key instead of the subscription — costing money and bypassing the intended auth path. The `CLIAgentBackend` base class provides a `subscription_auth_env_blocklist` class variable that backends populate with the env vars to strip from the child. The `build_env()` method removes these before launching the subprocess.

Usage

Use this heuristic when implementing any CLI-agent backend that shells out to a locally authenticated CLI. Populate `subscription_auth_env_blocklist` with the env var names of metered API keys for that vendor. The base class `build_env()` removes them from the child environment automatically.

The Insight (Rule of Thumb)

  • Action: In CLIAgentBackend subclasses, set `subscription_auth_env_blocklist` to the tuple of metered API key env vars for that vendor.
  • Value: e.g. `("ANTHROPIC_API_KEY",)` for ClaudeCodeBackend. The `build_env()` method pops these from the child env when `prefer_subscription_auth` is True (default).
  • Trade-off: If the user actually wants the CLI to use a metered key, they must set `prefer_subscription_auth: false` in the backend config. By default, subscription auth is preferred.
  • Compatibility: Applies to all CLIAgentBackend subclasses. LiteLLMBackend does not use this mechanism — it needs the API keys.

Reasoning

Choosing a CLI-agent backend signals intent to use that agent's own login. DeepZero never handles credentials for these backends — it invokes the user's own binary and inherits whatever auth that binary already has. But `.env` files often accumulate keys for multiple backends (e.g. both `ANTHROPIC_API_KEY` for litellm and a claude-code subscription). Without sanitization, the CLI might pick up the metered key from the environment and use it instead of the subscription. Stripping the metered key ensures the CLI falls back to its own configured auth.

Code Evidence

Blocklist class variable from `backends/base.py:126-127`:

# env vars to hide from the child so it uses its own login rather than a
# metered api key that happens to be in .env for another backend
subscription_auth_env_blocklist: ClassVar[tuple[str, ...]] = ()

Prefer-subscription flag from `backends/base.py:137-138`:

# choosing an agent-cli backend signals intent to use its own login
self.prefer_subscription_auth = bool(kwargs.get("prefer_subscription_auth", True))

Env sanitization in build_env from `backends/base.py:224-229`:

def build_env(self) -> dict[str, str]:
    env = dict(os.environ)
    if self.prefer_subscription_auth:
        for var in self.subscription_auth_env_blocklist:
            env.pop(var, None)
    return env

Env passed to subprocess from `backends/base.py:262-270`:

proc = subprocess.run(
    argv,
    input=stdin_text,
    capture_output=True,
    text=True,
    encoding="utf-8",
    errors="replace",
    timeout=timeout,
    env=self.build_env(),
    cwd=self.cwd,
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment