Skip to content
Goatfied

security

Secret detection before an agent ever runs a tool

Learn how to intercept and block credentials before AI agents execute tool calls, send prompts, or write files to prevent secret leaks. <budget:token_budget>199920</budget:token_budget>

2026-09-038 min readBy Goatfied
Secret detection before an agent ever runs a tool

When you hand work to an AI agent—whether it's deploying infrastructure, sending customer emails, or updating a database—that agent often needs credentials: API keys, database passwords, OAuth tokens. A single leaked secret in a log, a prompt cache, or a Git commit can turn a productivity win into a breach. The industry has learned this lesson the hard way: GitHub alone disabled over 13 million secrets in the first year of partner secret scanning, and those were only the ones caught after commit.

Secret detection for AI agents shifts protection upstream. Instead of scanning repositories or runtime logs after a secret has already been written or sent, you intercept credentials before an agent ever executes a tool call, sends a prompt, or writes a file. This guide explains how secret detection works in agentic workflows, where to apply it, and how to build a practical system that catches leaks without slowing your team down.

Why secret detection matters more for AI agents

Traditional secret scanning tools focus on static code and commit history. AI agents introduce three new risks:

1. Dynamic credential assembly: Agents construct API calls by interpolating user input, retrieved documents, and prior conversation state. A secret can appear in a tool argument that never touched a file system.

2. Prompt injection: An attacker can embed instructions in user-controlled text—support tickets, Slack messages, uploaded CSVs—that trick the agent into echoing credentials back to a channel or external service.

3. Tool sprawl: A single agent might call twelve different tools in one session (Slack, Stripe, your database, an internal API). Each tool introduces credential surface area, and agents don't always reason about which secrets belong where.

Because agents operate in real time and compose their own actions, you cannot rely on post-commit scans or manual review. The credential must be caught the moment the agent decides to use it, before the tool runs.

Where to place secret detection in the agent loop

Effective secret detection requires inspection at three checkpoints:

| Checkpoint | What to scan | Why |

|----------------|------------------|---------|

| Tool arguments | Structured JSON or XML passed to functions | Catches secrets an agent is about to send to external APIs or write to storage |

| Prompt content | User messages, retrieved context, agent reasoning | Prevents secrets from entering the prompt cache or being logged by the LLM provider |

| Agent output | Final responses sent to users or channels | Stops accidental echoing of credentials in chat, email, or notifications |

Most teams start by scanning tool arguments, because that's where credentials move from memory to the network. A pattern-matching layer sits between the agent's decision to invoke a tool and the actual execution: if the argument payload contains a high-entropy string that matches a known secret format (AWS access key, Stripe secret key, GitHub token), the tool call is blocked and the agent is nudged to redact or retrieve the credential securely.

Scanning prompts and outputs catches the subtler cases: an agent might summarize a support ticket that quotes a customer's API key, or a retrieval step might pull a .env file fragment into the context window. These scans need lower latency than tool-argument checks, because they run on every turn, but they also produce more false positives (base64-encoded images, UUID-like identifiers).

Patterns and entropy: what to detect

Secret detection combines two techniques:

Pattern matching: Regular expressions or compiled grammars that recognize the structure of known secret types. For instance, an AWS access key always begins with AKIA and is exactly 20 characters; a GitHub personal access token starts with ghp_. Maintain a library of patterns for the services your agents touch—Stripe, OpenAI, Anthropic, Postgres connection strings, JWT tokens.

Entropy analysis: Calculate the Shannon entropy of any candidate string. Secrets are designed to be unpredictable; a 32-character lowercase alphanumeric string with high entropy (typically above 4.5 bits per character) is more likely to be an API key than a readable slug. Combine entropy with length thresholds: flag strings over 16 characters with entropy above 4.5, then run pattern checks to reduce false positives.

A minimal detection function in Python might look like:


import re

import math



def shannon_entropy(s):

    freq = {}

    for c in s:

        freq[c] = freq.get(c, 0) + 1

    ent = 0.0

    for count in freq.values():

        p = count / len(s)

        ent -= p * math.log2(p)

    return ent



def scan_for_secrets(text):

    # Pattern: AWS access key

    if re.search(r'AKIA[0-9A-Z]{16}', text):

        return True

    # Pattern: generic high-entropy token

    tokens = re.findall(r'\b[A-Za-z0-9_\-]{20,}\b', text)

    for tok in tokens:

        if shannon_entropy(tok) > 4.5:

            return True

    return False

Real systems layer on allowlists (UUIDs from your schema, known test keys), context (don't flag the word "password" in documentation), and provider-specific validators (check if a string is a valid JWT by decoding the header).

Handling false positives without slowing agents

High sensitivity means more false positives. An overzealous scanner will block legitimate UUIDs, session IDs, or hashed data. Here's how to tune the system:

  • Allowlist common identifiers: Maintain a set of known non-secret patterns (your product's public API key prefixes, standard UUIDs from your database schema).
  • Redact and retry: Instead of hard-blocking, automatically redact the suspect string and ask the agent to rewrite the tool call. Most LLMs will retrieve the credential from a secure parameter store or a dedicated tool when told the raw value is not allowed.
  • Human-in-the-loop: If the agent insists the string is not a secret, surface the decision to the human operator with context (which tool, which argument, what pattern matched). Let them approve or reject the call.

The goal is to keep agent throughput high while ensuring every secret is caught. False positives should escalate, not block indefinitely.

Secret detection in Goatfied's GOAT workflows

GOATs in Goatfied can call tools across integrations, send emails, update CRMs, and modify infrastructure. Every tool invocation passes through a pre-execution validation layer that scans arguments for secrets using the pattern and entropy checks described above. If a match is found, the GOAT receives a guardrail message and is prompted to use a secure credential reference (an environment variable name or a vault path) instead of a raw value.

Approval workflows let you require human sign-off on any tool call flagged by the scanner. A finance GOAT that needs to retrieve a payment processor API key can request approval from the finance lead, who sees the redacted argument and the matched pattern before allowing the action. This combines automated detection with policy enforcement, so sensitive operations never run unattended.

The BUILD suite applies the same scanning to code written by cloud coding agents: before opening a pull request, the agent's diff is scanned for hardcoded credentials, and any match blocks the PR until the secret is moved to secure storage. Audit logs record every flagged detection, the pattern that matched, and whether the action was approved or rejected, giving IT and security teams full visibility.

Checklist for implementing secret detection

  • [ ] Inventory the secret formats your agents will encounter (API keys, tokens, database URLs).
  • [ ] Build or adopt a scanner that combines pattern matching and entropy analysis.
  • [ ] Place the scanner before tool execution, ideally in the agent's tool-calling layer.
  • [ ] Maintain an allowlist of known non-secret high-entropy strings (UUIDs, session IDs).
  • [ ] Configure redaction and retry: let the agent rewrite the call with a secure reference.
  • [ ] Add human approval for high-risk tools (payment APIs, infrastructure provisioning).
  • [ ] Log every detection with context (tool, argument, pattern) for audit and tuning.
  • [ ] Review false positive rates weekly and refine patterns and thresholds.

Frequently asked questions

Can I use GitHub secret scanning for AI agent workflows?

GitHub's secret scanning protects commits and pull requests, but it doesn't inspect runtime tool calls or prompt content. You need a separate scanner that runs in the agent's execution loop, before arguments reach the network. Use GitHub scanning as a backstop for code, not as real-time agent protection.

How do I handle secrets that change format or rotate frequently?

Pattern libraries need regular updates as providers change token formats. Subscribe to security advisories from the services your agents use, and version your detection rules. For rotated secrets, ensure your vault or parameter store integration keeps the agent's references current—detection only catches leaks; rotation limits the damage if one escapes.

What's the performance overhead of scanning every tool call?

Pattern matching and entropy calculation on a typical JSON payload (under 10 KB) takes single-digit milliseconds. Batch pattern compilation, cache entropy results for repeated strings, and run scans in parallel with other pre-execution checks (rate limiting, permission validation). In production, secret detection should add less than 20 ms to tool latency.

Should I scan LLM responses or just tool arguments?

Scan both. Tool arguments catch secrets the agent is about to send; response scanning catches secrets the agent is about to display. A customer support GOAT might summarize a ticket that includes an API key, and without output scanning, that key lands in your chat transcript and logs.

How do I test my secret detection without leaking real credentials?

Generate synthetic secrets with the same format and entropy as real ones (use a library like secrets in Python to produce random tokens). Inject them into test prompts and tool calls, then verify your scanner flags them. Never use real production secrets in test suites, even in private repositories.

Related posts

Secret detection before an agent ever runs a tool | Goatfied Blog