AI Security
Scoping API Keys and Service Tokens for Secure AI Automations
TL;DR: Treat every AI‑driven automation as a separate micro‑service. Create a dedicated API key or token for each agent, grant only the exact scopes it needs (e.g., read‑only CRM contacts, write‑only log bucket), store secrets in a vault, rotate them regularly, and monitor usage with alerts. This limits blast‑radius if a token is leaked and satisfies most compliance checklists.
Why Scoping Matters for Small Teams
AI agents often call external services—cloud storage, CRM APIs, email providers, or custom back‑ends. When a token has broad permissions, a single prompt injection or misbehaving model can cause data exfiltration, unwanted writes, or costly API abuse. For a startup with limited security staff, the simplest defense is least‑privilege: give the agent only what it truly needs.
Define the Agent’s Functional Boundaries First
Before you generate any secret, write down the exact actions the agent must perform. Use a table like the one below to clarify scope:
Agent | Required Action | API Scope
----------------|------------------------------|------------------------
Lead‑Enricher | Read contact info, write tag | crm.contacts.read, crm.tags.write
Invoice‑Summarizer | Upload PDF, read bucket | storage.buckets.read, storage.objects.create
Email‑Notifier | Send email via SendGrid | mail.send
Each row becomes a separate credential set.
Best Practices for Token Scope
- One token per agent. Avoid sharing a master key across multiple workflows.
- Use provider‑native scopes. Most SaaS APIs (e.g., Salesforce, HubSpot, OpenAI) support granular scopes; select the narrowest list that satisfies the table above.
- Prefer short‑lived tokens. If the provider offers OAuth2 access tokens with expiration, use them and store the refresh token securely.
- Store in a vault. Services like AWS Secrets Manager, HashiCorp Vault, or even Cloudflare Workers KV (with encryption) keep keys out of code repositories.
- Never hard‑code. Reference the secret at runtime via environment variables or secret injection mechanisms.
Implementing Least‑Privilege in Popular Agent Platforms
Below are concrete steps for three widely‑used managed‑agent platforms.
Claude Managed Agents
Claude’s platform lets you attach service_tokens to an agent definition. Define a token with the exact scopes in the Claude dashboard, then reference it in the agent’s environment block. Example:
{
"name": "lead‑enricher",
"environment": {
"CRM_TOKEN": "{{secrets.CRM_TOKEN}}"
}
}
Only the crm.contacts.read and crm.tags.write scopes are granted, so any attempt to delete a contact fails with an authorization error.
OpenAI Agents
When creating an OpenAI assistant with tool calling, you supply tool_resources. Attach a scoped API key as a tool parameter, and enforce the scope in the tool’s implementation. Example from the OpenAI docs:
assistant = client.beta.assistants.create(
name="invoice‑summarizer",
tools=[{"type": "function", "function": {"name": "upload_to_bucket", "description": "Upload PDF", "parameters": {...}}}],
metadata={"bucket_token": "{{secrets.BUCKET_TOKEN}}"}
)
Only the storage.objects.create permission is granted to the bucket token.
Zapier Agents
Zapier stores connections as “accounts”. Create a dedicated Zapier account for each agent, connect only the required apps, and generate a unique API key under My Apps → API Key. Then, in the Zap editor, select that account for the steps that need it. This isolates credentials per Zap.
Rotating and Revoking Tokens
Even with tight scopes, a leaked token must be revoked quickly.
- Automate rotation: schedule a nightly job that calls the provider’s token‑rotation endpoint, stores the new secret, and updates the vault.
- Invalidate on anomaly: set up alerts for unusual call patterns (e.g., >1000 requests/minute) and automatically revoke the token via the provider’s API.
- Document rotation procedures: keep a one‑page runbook in your internal wiki, linking to the vault entry and the rotation script.
Monitoring Token Use
Visibility completes the security loop. Add these lightweight checks:
- Log every API call with token identifier, endpoint, timestamp, and outcome. Send logs to a centralized SIEM or Cloudflare Logpush.
- Enable provider‑side audit logs (e.g., Google Cloud Audit Logs) and forward them to your monitoring system.
- Create a daily summary dashboard that shows token usage trends and flags any token with zero activity (possible orphan) or spikes.
For small teams, a simple cron job that queries the provider’s usage API and posts to a Slack channel is often sufficient.
When to Involve AISecAll
If you need a quick audit of existing token configurations, or want a managed secret‑rotation pipeline that integrates with multiple AI agents, AISecAll can design and implement a vault‑backed workflow tailored to your stack.
Need a practical AI security review?
AISecAll reviews prompts, tool permissions, document flows, and agent behavior so small teams can use AI without guessing where the risk sits.