Lesson 06 · Everyone

Managing API keys and tokens when agents are in the loop

about 6 minutes

API keys and access tokens are how software, including AI agents, proves it is allowed to use a service. They are also among the most commonly leaked secrets in modern businesses. This lesson explains what keys and tokens are, why agents make them easier to leak, and a small set of habits that sharply reduce the damage when one does get out.

Keys and tokens, in plain terms

An API (application programming interface) is the way one piece of software talks to another: your accounting system’s API lets other tools read invoices; your email provider’s API lets apps send mail.

An API key is a long secret string a service gives you so a program can use its API. Keys often last until someone deletes them.

An access token is similar, but usually issued for a limited time and a limited purpose, often through a “Connect this app” screen. The scope of a token, meaning which actions it allows, is usually set when it is created.

Most keys and tokens are bearer credentials: the internet standard for OAuth tokens defines a bearer token as one that “any party in possession of” can use, “without demonstrating possession of a cryptographic key” [6]. In other words, whoever has a copy can use it. There is no second check.

Why secrets leak, and how often

Secrets end up in places they shouldn’t: code, shared documents, chat messages, spreadsheets, screenshots. Researchers at North Carolina State University scanned billions of files on GitHub and found secret leakage affecting over 100,000 repositories, with “thousands of new, unique secrets” leaked every day [1]. The security company GitGuardian reports detecting about 23.8 million new secrets in public GitHub commits during 2024, up 25 per cent on the year before [2]. Those numbers only cover public code; private documents and chat logs are not counted.

Why agents raise the stakes

Agents add three new ways for a key to escape.

  1. The agent’s memory. If you paste a key into a chat, or a tool hands one back to the agent, it becomes part of the text the model reads. It can be repeated in an answer, included in a summary, or saved in transcripts. OWASP lists security credentials among the sensitive information AI applications can disclose [10].
  2. Instructions hidden in content. If the agent also reads emails, web pages or documents, hidden instructions in that content can ask it to send the key somewhere. That combination of private data, untrusted content and a way to send data out is what makes agents easy to exploit [9].
  3. Keys the agent creates or handles. Coding agents in particular write configuration files and scripts. A key can slip into a file that is later shared or committed.

Five habits that limit the damage

1. Keep keys out of the agent’s view

The best place for a key is somewhere the agent can use it without seeing it. In practice that means a password manager, a secrets manager or a connector that adds the key to requests on the agent’s behalf. Anthropic describes this design in its cloud coding agent: sensitive credentials are never placed inside the sandbox where the agent runs; a separate proxy checks each request and attaches the real credential [11]. If an agent never holds the key, a manipulated agent can’t leak it. Standards for agent connections are moving the same way: the Model Context Protocol’s authorisation rules require each server to accept only tokens issued for it, and forbid passing a token through to other services [13].

2. Give each agent its own key, with the smallest scope

Don’t share one powerful key between people, scripts and agents. Create a separate key for each agent and give it only what its job needs. GitHub’s fine-grained access tokens are a good model: each can be limited to specific repositories and given an expiry date [4]. The OAuth security guidance from the IETF recommends the same principle for tokens: restrict their privileges “to the minimum required for the particular application or use case” [7].

3. Prefer short-lived over long-lived

A key that stops working after an hour is far less dangerous to lose than one that works forever. AWS explains the benefit of its temporary credentials simply: they expire automatically, and once expired “they cannot be reused” [8]. OWASP’s secrets management guidance recommends generating credentials dynamically where possible, with short lease times, and revoking them when no longer needed [5].

4. Rotate, and know how to revoke

Rotation means replacing a key with a new one on a schedule, so a stolen copy stops working. OWASP recommends regular rotation “so that any stolen credentials will only work for a short time” [5]. NIST’s key management guidance makes the same argument in general terms: a defined cryptoperiod, the time a key is authorised for use, “limits the amount of exposure if a single key is compromised” [12]. Revocation means cancelling a key immediately. Before you give an agent a key, make sure you know where to revoke it, and that revoking it won’t break other systems (another reason for one key per agent).

5. Turn on leak detection

Many platforms can spot keys before or after they leak. GitHub’s push protection, for example, blocks a code push that contains a recognised secret and explains why [3]. Use these tools, and treat every alert as real: revoke first, investigate second.

A worked example

Ana runs a five-person design studio. Last year she created one API key for the studio’s project management tool and pasted it into an automation script, a spreadsheet of “useful logins” and an AI agent that writes weekly status reports.

What can go wrong. The agent reads client feedback emails while writing reports. A hidden instruction in one email asks it to include “all configuration values” in the report sent to the client. The key, with full admin rights and no expiry, goes out in an email.

What she changes.

Before After
One admin key shared by script, spreadsheet and agent Three separate keys, one per use
Key visible to the agent in its instructions Agent connects through the tool’s official integration; it never sees a key
Admin rights Agent’s access is read-only on projects
No expiry Keys expire every 90 days; calendar reminder to renew
Key in a spreadsheet Keys stored in the studio’s password manager only
No monitoring Leak alerts turned on in the code host and password manager

If the agent is tricked again, the credential stays hidden, but project data can still leak unless outbound sharing is controlled.

A checklist

  1. Make a list of every key and token in use, who created it, and what uses it.
  2. Remove keys from documents, chats and agent instructions; store them in a password or secrets manager.
  3. Give each agent its own credential, with the narrowest scope and an expiry.
  4. Prefer official “Connect” integrations and short-lived tokens over pasted keys.
  5. Turn on secret scanning and leak alerts where available.
  6. Write down how to revoke each key, and test it once.

Key takeaways

Most API keys and tokens are bearer credentials, so anyone holding a copy can use them, and leaked secrets are common enough to be measured in millions per year. Agents add new leak paths, because anything they can see may be repeated, logged, or extracted by instructions hidden in content they read. The strongest protection is an arrangement where the agent can use a credential without ever seeing it, backed by a separate, narrowly scoped and short-lived credential for each agent. Rotation, tested revocation and leak detection then make sure that when a key does escape, it is useful to an attacker for as short a time as possible.

Sources and further reading

  1. Meli, M., McNiece, M. R., & Reaves, B. (2019). How Bad Can It Git? Characterizing Secret Leakage in Public GitHub Repositories. Network and Distributed System Security Symposium (NDSS 2019). https://www.ndss-symposium.org/ndss-paper/how-bad-can-it-git-characterizing-secret-leakage-in-public-github-repositories/
  2. GitGuardian. (2025). The State of Secrets Sprawl 2025. https://www.gitguardian.com/state-of-secrets-sprawl-report-2025
  3. GitHub. (n.d.). About push protection. GitHub Docs. https://docs.github.com/en/code-security/secret-scanning/introduction/about-push-protection
  4. GitHub. (n.d.). Managing your personal access tokens. GitHub Docs. https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
  5. OWASP Cheat Sheet Series. (n.d.). Secrets Management Cheat Sheet. https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html
  6. Jones, M., & Hardt, D. (2012). The OAuth 2.0 Authorization Framework: Bearer Token Usage (RFC 6750). IETF. https://www.rfc-editor.org/rfc/rfc6750
  7. Lodderstedt, T., Bradley, J., Labunets, A., & Fett, D. (2025). Best Current Practice for OAuth 2.0 Security (RFC 9700). IETF. https://www.rfc-editor.org/rfc/rfc9700
  8. Amazon Web Services. (n.d.). Temporary security credentials in IAM. AWS Identity and Access Management User Guide. https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html
  9. Willison, S. (2025, 16 June). The lethal trifecta for AI agents: private data, untrusted content, and external communication. https://simonwillison.net/2025/Jun/16/the-lethal-trifecta/
  10. OWASP Gen AI Security Project. (2025). LLM02:2025 Sensitive Information Disclosure. https://genai.owasp.org/llmrisk/llm022025-sensitive-information-disclosure/
  11. Dworken, D., & Weller-Davies, O. (2025, 20 October). Beyond permission prompts: making Claude Code more secure and autonomous. Anthropic Engineering. https://www.anthropic.com/engineering/claude-code-sandboxing
  12. Barker, E. (2020). Recommendation for Key Management: Part 1 – General (NIST SP 800-57 Part 1 Rev. 5), Section 5.3 Cryptoperiods. NIST. https://csrc.nist.gov/pubs/sp/800/57/pt1/r5/final
  13. Model Context Protocol. (2025). Specification 2025-06-18: Authorization. https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization