Skip to main content

Settings and .env

madbench resolves its own configuration. You do not need to export keys before running it, and you do not need a shell wrapper that sources a dotenv file — madbench reads one itself, then tells you before spending anything if something is missing. On macOS it will also read a key you put in your login keychain, so a key need not be in a file at all.

Precedence​

Highest wins:

  1. Command-line flags — --concurrency=8
  2. The process environment — export ANTHROPIC_API_KEY=…
  3. The dotenv file — ./.env
  4. The macOS login keychain — madbench key set ANTHROPIC_API_KEY

A key already exported in your shell always beats the same name in ./.env. That is deliberate: it is the rule the shell idiom this replaced already had, so switching to madbench's own loader cannot change which key a run uses.

The keychain is the lowest source. It may only fill names the three above left unresolved — including a name a higher-precedence source deliberately defined as empty. That is not a detail, it is the whole reason a fourth source could be added at all: no configuration that worked before resolves a different value now.

If the file and your shell disagree, madbench says so, by name, without printing either value:

settings: ANTHROPIC_API_KEY is set in ./.env but a DIFFERENT value is already
exported in your shell — the shell value wins (--no-env to ignore ./.env)

That notice exists because the alternative is silent: a stale export beats a fresh file, preflight goes green, and you spend on the wrong credential.

The four states​

InvocationWhat is read
madbench …the process environment, ./.env if it exists, then the login keychain
madbench --env-file secrets.env …the process environment, secrets.env, then the login keychain — a missing file is an error
madbench --no-env …the process environment, then the login keychain
madbench --no-keychain …the process environment, then ./.env — no keychain source

--no-env and --no-keychain are two flags rather than one that switches off "the files", because the two sources fail in opposite ways (see The login keychain) — being able to drop one without the other is what tells a value's real source from its suspected source. Pass both for a run that resolves from the process environment and nothing else.

Off macOS the keychain source is absent from every row: it reports itself unsupported and supplies nothing.

./.env is read from the current working directory only. There is no parent-directory search: a .env one level up is not picked up, so a run's credentials never depend on where in a tree you happen to be standing.

A missing ./.env is fine — the file is optional. A ./.env that exists but cannot be read or parsed is a hard error, never a silent fall-back to a keyless run.

All three flags work on both forms: madbench <bench> and madbench preflight <bench>.

File grammar​

# a comment
KEY=value # trailing comments need a space before the #
PASS=abc#123 # ...so a # inside a value survives
export LEGACY=ok # an `export ` prefix is allowed and ignored
QUOTED='literal $no interpolation'
ESCAPED="line one\nline two"
MULTI="spans
lines"
EMPTY= # defined, with an empty value
  • Single quotes are literal — 'a\nb' is four characters.
  • Double quotes decode \n, \t, \r, \", \\; any other backslash pair is kept as-is.
  • Both quote styles may span lines.
  • No ${VAR} interpolation. A $ is a literal dollar sign. In a file whose whole purpose is secrets, expansion has no defensible precedence answer (would it expand from the file, or from the environment that also outranks it?) and silently mangles values that legitimately contain $.
  • A duplicated name takes its last value, like source ./.env.
  • Parse errors name a line number and never quote the file's contents, because in a malformed secrets file the unparseable text is often the secret.

What madbench looks up​

Provider keys, resolved per bench from the judges it declares:

NameUsed by
CLAUDE_CODE_OAUTH_TOKENthe claude-code Harness, billed against your Claude subscription (claude setup-token)
ANTHROPIC_API_KEYthe claude-code Harness (API billing — either credential satisfies it), and the default judge when a bench declares no judges: block
OPENROUTER_API_KEYopenrouter/… judges, the claudish wrapper
OPENAI_API_KEYopenai/… judges
any api_key_env a bench's judges: block namesthat judge

Tool overrides and policy: MADBENCH_MAGMUX, MADBENCH_BUN, MADBENCH_UV, MADBENCH_RUNTIME_INSTALL.

madbench's own knobs, each also a flag — and the flag wins: MADBENCH_CONCURRENCY, MADBENCH_TRIALS, MADBENCH_HARNESS, MADBENCH_UI, MADBENCH_SKIP_PREFLIGHT, MADBENCH_REPORT_JSON, MADBENCH_REPORT_JUNIT, MADBENCH_THEME (auto | light | dark — see ui-and-ux.md; this one is a property of your terminal rather than of a run, so it is usually worth exporting in your shell rather than committing to a bench's .env). (--param is not env-backed: one value cannot express a repeatable flag, and params belong in the Experiment file.)

TERM_THEME is honoured too — the cross-tool variable a terminal or multiplexer exports for every program on the session (light | dark; any other value, including auto, is ignored rather than rejected). It sits below --theme and MADBENCH_THEME and above asking the terminal. Unlike madbench's own knobs it is read from the process environment only, never from .env and never from the keychain, so it does not appear in .env.example.

.env.example in the repository root lists all of these with comments.

Any of these names can live in the login keychain instead of a file — the resolver looks up whatever name a bench asks for, and never consults a fixed list of known keys.

What reaches the agent​

Only the keys a bench actually resolves. madbench forwards a named allowlist — the base provider keys plus every resolved judge key — into the scenario sandbox. The rest of your .env never enters the agent's environment, and never reaches captured session logs, which redact by name. The login keychain source follows the same rule, at every sandbox level.

This matters because the agent under test runs arbitrary code. A .env holding forty unrelated production secrets forwards none of them.

Diagnosing "my key isn't being picked up"​

madbench preflight path/to/bench.yaml -v

-v prints the sources before the checklist:

SETTINGS process env, ./.env, login keychain
ok ./.env (named pipe) — 4 names
ok login keychain — 4 names
! ANTHROPIC_API_KEY also exported in your shell with a different value — the shell value wins

The header names every source consulted, not every source that happened to supply something — the same rule that names ./.env when the file defines nothing. A keychain that would not open was still consulted, so it still appears there, and its row says what went wrong.

The keychain row is one of these four. None of them prints a value:

ok login keychain — 4 names
ok login keychain — 4 names, some of them from a bundle item
• login keychain — nothing stored (madbench key set NAME)
! login keychain — login keychain: list items: no answer within 5s; the keychain may be locked

Under --no-keychain the row names no source at all, because none was read:

! the login keychain source is disabled (--no-keychain)

Off macOS there is no keychain row, and the header stays process env, ./.env. A line reporting "unsupported" on every Linux run would be a fact nobody can act on.

Then the checklist itself names any missing dependency, what needed it, and how to fix it — before any spend:

PREFLIGHT BLOCKED swe-mini · 5 components · 1 blocking
▌ BLOCKING claude credential
▌ neither CLAUDE_CODE_OAUTH_TOKEN nor ANTHROPIC_API_KEY found in
▌ process env, ./.env, login keychain, and no usable Claude Code
▌ login on this machine (sandbox levels home and container give
▌ the run its own HOME, so the login in ~/.claude is not visible
▌ inside it)
▌ needed by: Harness claude-code
▌ fix: run `claude setup-token` and export
▌ CLAUDE_CODE_OAUTH_TOKEN=… to bill against your
▌ subscription, or export ANTHROPIC_API_KEY=… for API
▌ billing (either works in ./.env, or via `madbench
▌ keychain set <NAME>`)
✗ 1 blocking problem, 0 advisories
nothing was run, no spend · fix them, or bypass with --skip-preflight

That is madbench preflight piped at 80 columns, the plain form. On a terminal the same lines carry the run's palette: the outcome is a badge, and each finding hangs off a gutter in its severity's colour. The block wraps to the terminal width, and every continuation stays under its own column, so a long message never runs back to the left edge.

The fix offers one clause per place this machine would actually read the key back out of. On Linux, or under --no-keychain, the keychain clause is absent: advice naming a source madbench will not consult is worse than no advice, because you store the key, the next run still fails, and the message you followed is the reason you trust that it should not have.

Exit codes: 0 clean · 1 the bench could not be loaded or configured · 3 preflight found blocking problems and nothing was run.

The login keychain​

macOS only. A key stored in your login keychain is available to every bench in every directory, is never written to a plaintext file, and needs no external tool — the store is the one your Mac already unlocks when you log in.

madbench key set # no NAME: pick from the keys madbench reads
madbench key set ANTHROPIC_API_KEY # prompts; the typed value is hidden
madbench key list # names, a mask and a date — never a value
madbench key get ANTHROPIC_API_KEY # one value, in full, on stdout
madbench key rm ANTHROPIC_API_KEY

set reads the value from stdin when stdin is a pipe or a file, and from a hidden prompt when stdin is a terminal. Where the value comes from is decided by what stdin is, not by a flag, so one command serves both a person pasting a key they just copied and a script piping one out of a secret manager:

op read "op://vault/anthropic/credential" | madbench key set ANTHROPIC_API_KEY

There is no --value flag, and there will not be one. A secret in an argument is readable by every other user on the machine with ps for as long as the command runs. Nothing madbench stores ever reaches an argument list.

list never prints a value. Its mask is a fixed width, so it does not even report how long a value is; the last few characters are there only so you can tell one key from another. get is the explicit opt-in for the real thing, and it is a separate command for exactly that reason.

Items are stored under the service name madbench, so Keychain Access shows them grouped under one heading and nothing else on the machine is touched. These commands resolve no bench and need no key of their own, so they never open ./.env — storing an unrelated key never costs a secret-manager approval.

Two storage shapes, one source​

The source is assembled from two kinds of item under that one service name:

ShapeHoldsWritten by
per-name itemone value, under the setting's own namemadbench key set
the bundle itemmany settings at once, as a blob in the same grammar ./.env uses — comments, quoting, export prefixes and allwhatever put it there; madbench reads it, see below

Where both define the same name, the per-name item wins. The bundle is a bulk import pasted once; the per-name item is a deliberate act aimed at one key, and the narrower, later statement is the one that should count. It also means you can override a single entry of a bundle without rewriting the whole blob.

The bundle is an item stored under the service name madbench with the account name @bundle. That leading @ is the first thing the setting-name grammar refuses, which is what lets one enumeration tell the two shapes apart with no marker attribute and no reserved-prefix table to keep in step.

madbench key set does not write the bundle — it refuses that name and says so, because the command stores one setting at a time. The bundle is for something else that provisions a machine in one act; madbench reads it, lists it and can remove it, but the only thing it writes is a per-name item. A listing shows the bundle as a row reporting how many names it declares, rather than a mask, so you can see it is worth keeping without seeing a character of it.

A keychain that will not open is never fatal​

A keychain that is locked, absent, or unreadable yields an empty source plus a diagnostic. Every other source resolves normally and the run continues.

This is deliberately the opposite of the ./.env rule, where a file that exists but cannot be read is a hard error. The two are not inconsistent:

  • ./.env is a file you pointed madbench at. If it is broken, the run you asked for cannot happen, and continuing would hand you a keyless run wearing a green checkmark.
  • The keychain is a fallback nobody named. If it will not open, the only thing that follows is that the fallback is empty. A run whose keys came from your shell must not fail because a store three sources down would not answer.

When the approval prompt comes back on every run​

An approval that does not stick is almost always a second gate you were never shown. A keychain item carries two independent lists, and the dialog you answered updates only one of them.

the ACLthe partition list
Holdsthe applications trusted to use the itemthe access groups allowed to use it
Setby security add-generic-password -T <path>, and by the dialog's Allow buttonsonce, when the item is created, from the creating process — /usr/bin/security creates items as apple-tool:
Changed by "Always Allow"yesno
Resetwhen the item is deleted and recreatedwhen the item is deleted and recreated

"Always Allow" writes to the ACL and never to the partition list. That is the whole of why an approval can appear not to stick: you granted what the dialog offered, and the gate that stopped the read is one the dialog never mentioned.

The second row of that table has a consequence worth stating on its own. An application that rewrites its own item by deleting it and adding it back resets both lists, so every reader of that item faces the prompt again — an approval you gave yesterday was attached to an item that no longer exists. Claude Code does this to Claude Code-credentials when it refreshes its login (a known upstream bug, anthropics/claude-code issues 62213 and 81707), and that is the item madbench reads for use_subscription runs (harness.md, "the host login"). The prompt returning once per run is the symptom; nothing on madbench's side changed between the runs.

Repairing one item takes a command you have to run, because it needs your keychain password and madbench will never ask you for it:

security set-generic-password-partition-list \
-S apple-tool:,apple: -s "Claude Code-credentials" -a "$USER"

Which of the three dialogs you are looking at tells you which gate you hit:

The dialog saysPassword fieldWhat it means
"enter the keychain password to unlock the login keychain", with no Always Allowyesthe keychain is locked
"… wants to use your confidential information stored in …", Allow / Always Allownoan ACL miss
the same wording, Allow / Always Allowyesthe partition-list gate — this is the one that repeats

madbench's own items under the madbench service are not subject to this. madbench keychain set passes -T /usr/bin/security only when it creates an item. Applying -T to an item that already exists sends the tool down its set-access path, and setting an item's access asks a person for the keychain password; an update needs no grant at all, because the access written when the item was created still stands. That asymmetry is the grantToolAccess parameter of addGenericPasswordCommand in pkg/settings/keychain_write.go.

None of this can hang a headless run. Every keychain read on the resolve path is bounded by the five-second budget described below, and for the host login madbench stops asking after the first failure — so a keychain that will not open costs one dialog per run rather than one per agent launch (stopAfterFailure, pkg/harness/claudecode/subscription.go).

Why the read has a deadline​

Every keychain read on the resolve path runs under a five-second budget. This is not a performance knob.

A locked keychain does not return an error. It blocks on a GUI authorization dialog, indefinitely, on a machine that may have nobody sitting at it. Without a deadline a headless run — CI, a scheduled eval, an ssh session — hangs forever waiting for a prompt no one will ever answer. The budget is what converts that hang into the degradation above. It bounds the whole source rather than one call inside it, so the worst case stays the same however many items you store.

madbench key set and its siblings get a far more generous deadline, for the opposite reason: you typed them, you are at the machine, and an unlock dialog you have to answer is a normal step of storing a key rather than a hang.

Two readers, one store​

A keychain value is madbench's own credential, and it follows exactly the rule a ./.env value follows: it crosses into a scenario sandbox only through the named forward-env allowlist, at every sandbox level — including the ones that forward the rest of your environment wholesale.

That distinction matters because two different processes read the login keychain on a Mac running a bench:

  • madbench's settings resolver reads it, finds a value, and hands that value to the agent in the agent's environment. The agent never learns which source produced it.
  • the agent under test never reads it to authenticate. The claude CLI under --bare reads ANTHROPIC_API_KEY or an apiKeyHelper and nothing else (harness.md). Separately, at any sandbox level below container the agent can reach your keychain the way it reaches any other file on your machine — but that is the agent acting on its own, not madbench handing it anything (sandbox-levels.md).

So "madbench reads the login keychain" and "the agent never reads the keychain" are both true, about different processes. See Keychain.

Keychain, or a mounted .env pipe?​

Both keep keys off disk, and the next section describes the pipe in full. They answer different questions:

login keychain.env as a mounted pipe
PlatformmacOS onlyanywhere the secret manager runs
External toolingnone — the store ships with the OSa secret manager, configured for this repository
Approval per runnone on the read pathyes — and that gate is the point
Shared with a teamno — your login, this Macyes, through a shared vault
Scoped per repositoryno — one store, every directoryyes — one mount per repository

Prefer the keychain when the keys are yours, the machine is yours, and you want a run to work from any directory with nothing to configure. Prefer the pipe when the keys belong to a team vault, when someone must approve each use, or when the same setup has to work on Linux.

Keeping secrets off disk: .env as a mounted pipe​

./.env does not have to be a regular file. A secret manager can mount it as a named pipe (FIFO), so your keys are never written to disk at all — and, more usefully, so that every read passes through an approval gate. 1Password does this natively; you point it at the repository and it mounts .env for you. There is no shell loop to run and no mkfifo to type: ls -l simply shows the file with a p mode and zero size.

madbench is built for this. It reads the dotenv file exactly once, eagerly, with a single open — so one run costs one approval.

What follows from a pipe, and what to expect:

  • One read per invocation. A second open would have no writer behind it, so it would block; madbench never does one.
  • The read can prompt. Approving is the point — it is what makes the gate meaningful. If the prompt is left unanswered, the open blocks until it is, exactly as sourcing the file in a shell did before. --no-env skips the source entirely if you want a run that cannot prompt.
  • Commands that need no settings never open it. madbench version, list and init resolve nothing, so they never cost an approval.
  • An empty read resolves zero names; madbench preflight -v prints the count, which is the quickest way to tell "the gate was declined" from "the file is empty".

An alternative that needs no dotenv source at all: inject the keys into the process environment and let madbench read them from there, where they outrank the file anyway —

op run --env-file=.env.op -- madbench path/to/bench.yaml

In CI, use a plain file and --env-file secrets.env. On a Mac that needs no vault at all, the login keychain is the third answer.

Do not add go-task's dotenv: key to Taskfile.yml, and do not re-add a . ./.env line to a task. Either one opens the pipe a second time, after which madbench's own read blocks or sees nothing — and go-task's dotenv parse errors echo file content, which is how key material ends up on your terminal.