Credentials by reference, environments built rather than inherited, and screening what an agent wrote before it is saved.
For your capstone Run a coding agent you don't have to trust
Your agent holds exactly one credential, its own provider key, and the controller decides that at every launch rather than leaving it to how you happened to start things. The version-control and deploy credentials stay with the controller, which commits and publishes the agent's work itself.
Say your shell holds a deploy token in DEPLOY_TOKEN when you start the controller. The run's grant names one reference, provider_key, and the controller builds the agent's environment from an allowlist: PATH, LANG and the resolved key. DEPLOY_TOKEN is not on the list, so the agent has none to print. Later it writes a fixture holding the provider key it can read; the screen finds that registered value before anything reaches the checkout, raises a critical finding, and the run cannot complete.
Check your own machine
These commands only read information — they change nothing. Run the
block for your OS, then use the table to read your own numbers
against the ideas above.
What you should see The first line prints one number: how many environment variables this PowerShell session holds. The second prints how many of their names contain token, key, secret or pass, counting names only and never printing a value; 0 is a perfectly good answer. The third starts a child cmd.exe, has it list the environment it inherited with set, and counts the lines without printing them; the number comes out close to the first, because the child received the session's variables.
Note cmd.exe can add or hide a few variables of its own, so the two counts may differ slightly. Stock PowerShell has no one-line equivalent of env -i; on Windows a program gets a different environment only when its parent builds one and passes it to the new process.
What you should see The first line prints one number, padded with spaces by the macOS wc: roughly how many environment variables your shell hands to every program it starts. The second prints how many variable names contain token, key, secret or pass: cut -s keeps only the part of each line before the first = and skips any line with no = in it, such as the extra lines of a multi-line value, so it counts names and never prints a value; 0 is a perfectly good answer.
Note Only exported variables are passed to children. Terminal apps and shell profiles add their own variables, so the count differs between a terminal window and an editor's built-in terminal.
What you should see The first line prints 0 (with leading spaces): env -i starts the program with an empty environment. The second prints exactly one line, GREETING=hello: a built environment holds only what was named.
What you should see The first line prints one number: roughly how many environment variables your shell hands to every program it starts (a variable whose value spans several lines adds extra lines). env is itself a child of your shell, so what it prints is exactly what a child received. The second line prints one number: how many variable names contain token, key, secret or pass. cut -s keeps only the part of each line before the first = and skips any line with no = in it, such as the extra lines of a multi-line value, so it counts names and never prints a value; 0 is a perfectly good answer.
Note Only exported variables are passed to children, so a variable you set without export in this shell does not appear. An agent started from this shell would receive everything counted here.
What you should see The first line prints 0: env -i starts the program with an empty environment, so it inherited nothing. The second prints exactly one line, GREETING=hello: a built environment holds only what was named.
Where each idea shows up in your output
Concept
Windows
macOS
Linux
A child receives a copy of its parent's environment
(cmd /c set).Count counts what the child cmd.exe inherited
env run from the shell prints what it inherited; env | wc -l counts it
env run from the shell prints what it inherited; env | wc -l counts it
Credential-looking names a launched agent would receive
Get-ChildItem Env: filtered with Where-Object, .Count
env | cut -s -d= -f1 | grep -ciE count
env | cut -s -d= -f1 | grep -ciE count
A built environment holds only what was named
—
env -i /usr/bin/env prints nothing; env -i GREETING=hello prints one line
env -i /usr/bin/env prints nothing; env -i GREETING=hello prints one line
The same ideas, as prose
These are the exact fragments the model serves — also available as an
ordered study guide.
The agent can read what its process holds
A credential is live authority to act: whoever holds a token can do whatever the token allows. An agent is a program that calls a language model in a loop, running the tools it requests, and a coding agent's tools include running commands. Put those together and you get the fact this concept is built on. An agent that can run commands can read anything its own process holds: its environment variables, the arguments it was started with, every file it can open. If a secret is in any of them, one command prints it, and an agent misled by text it read can be told to run that command. An instruction not to look is a request. What the process holds is a fact.
So the values stay with the controller, the program that starts the agent, holds the credentials, runs the checks and decides what happens next. Four habits follow from that. Secrets are passed by reference: records and configuration carry a secret's name, and the value exists only inside the controller between looking it up and launching the process that needs it. The agent's environment is built for the run from a grant that names what this one run needs, never inherited from whoever started it. The one credential the agent must hold, the key its runtime uses to call the model provider, is its own: dedicated, narrowly scoped and capped at the provider. And everything the agent wrote is screened for known secrets before it is saved or published.
There is a second direction as well. Whatever the agent reads, prompts, file contents, tool results and logs, is sent to the model, so a secret or a piece of personal data in that text has left your system.
None of this makes an agent safe. The agent can read the key it is given, and a screen can miss a secret written in a form it does not recognize. What these habits do is keep the credentials worth stealing out of the agent's reach, and keep the one it does hold small, capped and easy to replace.
The environment is not a hiding place
The usual advice for a running program is sound: keep the secret out of the code and supply it from the environment at run time. The twelve-factor approach stores this kind of config in environment variables, and a pipeline injects a secret into a step as an environment value when the step runs. For a web server or a deploy job that is the right shape. The program reads DATABASE_PASSWORD, uses it, and the value never touches the repository.
Two facts change the picture for an agent. First, a child process receives a copy of its parent's environment by default. The Linux manual says so of fork, and Windows documents the same default for a new process unless the parent passes a different environment block. Everything your shell exports, every program it starts holds too, including an agent and every command the agent runs. Second, a process can read its own environment: env prints it, and an agent that can run commands can run env. Starting an agent from your own shell is like lending someone your jacket with the house keys still in the pocket: you meant to lend the jacket, and the keys went with it.
Field recordIn one real system, a headless coding-agent run was launched with the parent's whole environment, so the child received every credential its caller held. Nobody decided to give the agent those credentials; they came along with the launch. The same review found a second, separate failure: the agent's runtime had ignored the permission configuration it was given. The fix was an exact allowlist for the child's environment, with credentials granted only by capability group.
So the environment advice holds, with a precise scope. Injecting a secret at run time into the environment of the process that uses it is still right, for a web server, a deploy job and the controller itself. An agent adds one fact: whatever its process holds, any command it runs can read, and a misled agent runs what it is told. So a credential goes into the agent's environment only if the agent may hold it. For a coding agent that means its own provider key and nothing else. Every other credential is injected into the controller, or into the fixed code that uses it, where the agent cannot look.
Build the environment from the grant
Inheriting would copy every variable in the caller's environment, credentials included, into the agent's process, so that path is not used; instead the agent's environment starts empty, the run's grant names an allowlist and one secret reference, the controller's resolver looks up only that reference, and the agent is launched with exactly those variables, so a credential the grant does not name is absent rather than removed.
Building an environment means starting from nothing and adding what the run needs, instead of starting from everything and trying to remove the dangerous parts. The command-line form of the idea is env -i, which runs a program with an empty environment: env -i GREETING=hello /usr/bin/env prints one line, GREETING=hello, and nothing else. A controller, the program that starts the agent and holds the credentials, does the same thing in code for every process it launches. An exact allowlist of ordinary variables goes in, and nothing is carried over from whoever started the controller.
Credentials enter through the grant, the controller's record of what one run may use. A grant names secrets by reference and hands them out by capability group, so a run that only edits code gets the model provider key and nothing else, while the credential that publishes or deploys belongs to fixed controller code that the agent's run never touches. At launch the controller resolves each named reference, places the value in the new environment and starts the process. A credential the grant does not name is simply absent, so there is nothing to strip and nothing to forget. Building the environment this way is like packing a bag for one trip from a written list, rather than bringing everything you own and trying to remember what to leave behind.
The rule covers more than the agent. One real system runs its own version-control commands with an environment built from scratch as well: inherited version-control and host variables are stripped and repository paths are given explicitly, so a variable left in the operator's shell cannot change what the command touches. An allowlist also fails safe as things change. A new token added to your shell profile next month is not on the list, so it never reaches the agent; a deny list would have needed someone to remember to add it.
A built environment decides what the agent's process holds. It does not decide which files the agent can see. That belongs to the sandbox, the contained environment its commands run in, whose filesystem view is built the same way, with credential directories left out.
Hold the name, not the value
The run store, records and events, API responses, logs, backups and configuration each hold only a secret's reference name; the value is read from a secret store or owner-only file into the controller's resolver at lookup, exists on the controller's side only until launch, and is then handed to the launched process, which holds it from then on, so a leaked reference reveals nothing and the value never rests in a record.
A reference is a secret's name, such as provider_key; the value is the secret itself. The design rule is that a value exists in as few places, for as short a time, as possible. In one real system, on the controller's side, secret values exist only inside its resolver, from the moment a secret is looked up to the moment the child process that needs it starts, and briefly while output is being redacted. After that, the value lives in the process that received it. Everything else on the controller's side holds references: the database of runs, records and events, API responses, backups, logs and configuration. A run that is paused and resumed carries the reference forward, never the value. A reference on its own is like a coat-check ticket, which names your coat without being it, so a ticket dropped in the street gives a stranger nothing to wear.
Where a value has to rest, it rests as a file under strict rules. In that system, deploy secrets are files readable only by the owner, kept outside every sandbox mount and delivered only over the controller's own launch channel. They are never placed in service properties, in command-line arguments or in files a sandbox can read. Arguments are a particularly poor hiding place: on a default Linux system any user can read another process's full command line from /proc/<pid>/cmdline, while the environment file sits behind a stricter access check. The controller also refuses a secret file that is a relative path, a link, a pipe, readable by its group, oversized, longer than one line, or empty, since each is a way for the file to be something other than the single secret its owner put there.
Two smaller rules come from the same idea. Configuration is written only by the owner, outside the repository the agents write, so an agent cannot plant a setting that changes where a secret goes. And when the controller needs to tell whether a configuration has changed, it identifies it by a keyed hash, never a plain hash of the secret. Anyone who sees a plain hash can hash likely guesses and compare; a keyed hash cannot be checked without a separate key the controller keeps to itself.
A transcript is somewhere a secret can leak
A conversation with an agent is a record. It is kept, fed back into every later turn, often written to logs, and sent to the model provider each time the model is called. A secret pasted into one is in all of those places at once, and the only safe assumption is that it has leaked.
Field recordIn one real production project built quickly by agents, a version-control host token was pasted into a chat transcript, a throwaway administrator password was published in an onboarding document, and a database password lived in two places. None of them was rotated. Three credentials were exposed or scattered, and nothing was done afterwards that would have made any of them worthless.
Field recordIn a later, separate system, the owner pasted a temporary access token into an orchestrating agent's conversation. It was flagged for revocation, because once pasted it sat in the transcript. The token being temporary did not change the response; it only shortened how long the exposure would have lasted on its own.
The fix is the one any leaked credential gets: rotate it. Deleting the message, like deleting a committed secret, removes it from one view and from nothing else. Revoking the credential and issuing a new one is what makes the exposed copy worthless. Better still is to give nobody a reason to paste. If a run needs a credential it does not have, that is a change to the run's grant, made by a person through the controller, recorded by reference and never typed into the chat.
A key of its own, capped where the money is
There is one credential the agent does need: the key its runtime uses to call the model provider. Make that key its own. Give the controller a dedicated API key for each provider, separate from the key you use by hand and from any key your app uses, with the narrowest scope the provider offers. A separate key can be capped, watched and revoked without touching anything else, and whatever it spends belongs to the agent. Switching a credential on is a human act, not something the controller or an agent does for itself. And a test environment holds only disposable secrets scoped to that environment.
Find out how the key actually reaches the agent's runtime by running it, not by guessing from a variable name you spotted in its files. One real system's rule is to establish by a test run whether the backend takes the key from an environment variable or from a file, since a wrong guess puts the key somewhere you did not plan for.
Put a hard cap at the provider, on a project or workspace that holds only the agent's dedicated key. The controller's own budget checks run between steps, and a single model invocation can run past them until its deadline ends it, so the controller records the provider cap as configured, not as something it enforces itself. A budget alert on the account is a warning, not a stop; a hard limit rejects requests once it is reached. Check what your provider actually enforces; the page on why the controller holds the authority covers where such caps sit and how far they can lag. Set up this way, the key works like a prepaid card loaded for one errand: if it is lost or misused, the damage stops at the limit the issuer set, no matter who is holding it.
Be plain about what this does not do. The agent holds the key, so anything it runs can read it. It can copy the key into its workspace, encode it so it no longer looks like a key, or send it to any host the sandbox allows. The usage the runtime reports is evidence attributed to the run, not an independent meter of everything the key could spend. A stronger design injects the credential at the egress proxy, so the agent's process holds only a placeholder and the proxy adds the real key to approved requests. That keeps the value away from the agent but not the authority, since any request the agent may send still spends it, and one real system deferred it until it could be qualified. Until then, the narrow scope, the provider's cap and the ability to revoke are what bound a leaked key.
Screen what it wrote before anything keeps it
The agent's writes wait in memory with no swap while a screen checks contents and names for every registered secret in raw and escaped forms, including secrets split across chunks; clean writes go on to the real checkout and stored records, a hit blocks both and raises a critical finding, and a key encoded into an unregistered form passes the screen unseen, which is the stated limit.
Anything the agent writes might carry a secret: a log line that echoed an environment variable, a config file it filled in helpfully, a test fixture, an error message. So in one real system, nothing the agent wrote is kept until it has been screened. The agent's writes stay in memory-backed storage, excluded from swap so they are not written to disk, until the controller has checked them for every registered secret, meaning every value it knows because it resolved that value itself. The screen looks for each value in its raw form and in escaped forms, and it checks file and directory names as well as contents. It works like a mailroom that checks every outgoing envelope for marked documents before anything leaves the building, and holds the whole batch when the check cannot run.
A hit stops two things at once: copying the changes into the real checkout, and storing the run's output as a record. It raises a critical security finding, and the run cannot complete. The redactor that cleans streamed output also catches a secret split across two chunks of the stream or across the bytes of a single multi-byte character, the places a filter that looks at one piece at a time would miss. When a new secret is registered, stored records are scanned again for it, and any evidence that depends on a hit is quarantined.
The screen has to cover more than the obvious outputs.
Field recordIn one real system, a review confirmed by running it that a secret the agent held could reach the controller's database and its event API inside the raw destination text of a refused proxy connection. The proxy did its job and refused the connection, and the record of that refusal carried the agent's text, secret and all, into storage. Now every stored reason built from text the agent controls is screened before it is saved.
A field the agent can influence is agent output, even inside a record the controller wrote.
If you cannot redact, do not publish
Screening has a failure mode of its own: the moment it cannot run. The rule for that moment is short. If redaction cannot be assured, publish nothing. In one real system, a crash before collection loses the agent's in-memory writes instead of leaving unscreened secrets on disk, and the design accepts that the agent's unsaved changes are lost with them. After a controller restart, output from a service that survived the restart is withheld and marked redaction unavailable until the service is replaced, because the restarted redactor may not know the secrets the old one was screening for. Lost work can be redone. A secret published into a record that people and agents read cannot be taken back, only rotated.
Then there is what the screen cannot see even when it runs. It finds registered values in their raw and escaped forms, and that is all. The design record says it plainly: a key-holding role can encode its key in a way no detector recognizes. A key split into pieces or converted to base64 is the same key to anyone who reassembles or decodes it, and a different string to the screen. Secrets the owner types in a form that was never registered, a password pasted into a task description, reach the model provider untouched, because nobody told the screen they were secrets.
General-purpose secret scanners do not close the gap. They combine known token formats, nearby keywords and a measure of how random a string looks, and a 2026 study that ran three popular open-source scanners with their default rules found they caught most Base64-encoded test credentials but missed many that were split into two parts, and none flagged credentials committed in encrypted form. A clean scan means nothing matched the rules that ran, not that nothing was there.
That is why redaction is a best effort and not the boundary. The screen catches the accidental leak, the echoed variable and the helpful config file. The deliberate or injected leak is bounded by the other habits: the agent holds one key, that key is narrow and capped at the provider, and it can be revoked as soon as anyone suspects it has left.
Whatever you send has left your system
Everything the agent reads goes to the model. The prompt, the files it opens, the output of every command it runs, the tool results and logs it is handed: each becomes input to a model call, and with a hosted model that call leaves your system. Whatever was in the text is then held under the provider's terms, not yours. The UK National Cyber Security Centre made the point in 2023: a query is visible to the organization providing the model, stored queries can be leaked or accidentally made public, and the terms of use and privacy policy need to be understood before anything sensitive is sent.
OWASP's 2025 list of the top risks for language-model applications includes sensitive information disclosure, and it says separately that a system prompt should not be treated as a secret or used as a security control, so credentials and connection strings do not belong in one. The same holds for tool results. A token inside an error message has just become part of the conversation, and from there it goes wherever the conversation goes.
Three principles cover most of it. Send the minimum the task needs: the failing test rather than the whole suite's output, the relevant stretch of a log rather than the whole file. Redact before the agent reads, not after: strip secrets and personal data from logs, tickets and tool results on the way in, because once the model has read a value it can repeat it anywhere its output goes. And know your provider's retention terms instead of assuming them. Providers differ in how long they keep prompts and outputs, whether they keep them to monitor for abuse, whether they use them for training, and whether the answers change between a consumer chat product and API access or with the features you switch on. The terms for the exact product and account you use are the ones that apply.
This direction has the same limit as any screen. Redaction on the way in catches what it recognizes and nothing else. So treat anything that reaches the model as disclosed, and decide what may be sent with that in mind.
Nothing worth stealing in the sandbox
The capstone is to run a coding agent you do not have to trust: put it to work on real code where the controls around it, not its good behavior, keep you safe. Secrets are where that promise breaks most quietly. A leaked credential raises no error, and it keeps working until someone notices and revokes it.
Here is what a sensible setup looks like. The controller starts each run from a grant. The grant names one secret reference, the agent's dedicated provider key, and the controller resolves it at launch into an environment built from an allowlist, so the tokens in your own shell never reach the run. That key has a hard cap set at the provider and can be revoked in one step. Version-control and deploy credentials stay with the controller, which commits and publishes the agent's work itself. The agent's writes sit in memory until they have been screened for every registered secret, and a hit stops the run before anything is kept. Logs and tickets are trimmed and redacted before the agent reads them, because whatever it reads is sent to the model.
Run that setup and be clear about what you still accept. The agent can read its own key, and it could encode the key past the screen or send it to a host the sandbox allows. What you have done is make that the worst case for credentials: one narrow, capped, replaceable key, instead of every credential you hold. The code and anything ever committed in the repository's history are still within its reach, which is why a committed secret is rotated, not just deleted. Among credentials, a misled agent finds little worth stealing, and the one key it could take is bounded and quick to revoke.