AI Security
How to Scope API Keys and Service Tokens for Secure AI Automations in Small Teams
TL;DR: Treat every AI integration as a separate micro‑service. Create a dedicated API key or token per integration, grant only the exact scopes required (read, write, or invoke), store secrets in a secret manager, rotate them regularly, and audit usage weekly. This limits blast‑radius if a key is compromised and keeps compliance simple.
Why Scoping Matters for Small Teams
Small companies often use a handful of AI tools (e.g., OpenAI’s GPT, Claude’s managed agents, Cloudflare Workers AI) to automate tasks such as content generation, ticket triage, or data extraction. Those tools are accessed via API keys or service tokens that, if over‑privileged, become a single point of failure. The OWASP GenAI Security Project lists “Excessive Privilege” as a top risk, and the NIST Trustworthy AI framework recommends “least‑privilege” as a core control.
Step 1 – Inventory Every AI Integration
Start with a spreadsheet that captures:
- Provider (OpenAI, Anthropic, Cloudflare, etc.)
- Purpose (summarization, code review, chatbot)
- Required actions (read‑only, write, invoke, file upload)
- Current credential (API key, OAuth token, service account)
Having a clear map prevents accidental sharing of a single key across unrelated workflows.
Step 2 – Choose the Right Credential Type
Different platforms expose different granularity:
- OpenAI Agents – Supports
openai.api_keywith optionalorganizationheader to limit usage to a specific org. Use Agent‑specific keys when available. - Claude Managed Agents – Allows you to create managed‑agent tokens scoped to a single agent and its configured tools.
- Cloudflare Workers AI – Uses a Cloudflare API token that can be limited to the
Workers AIservice and to specific account zones.
Whenever the provider offers a “service token” or “agent token” that is narrower than a global API key, prefer it.
Step 3 – Define Minimal Scopes
Map the required actions from your inventory to the provider’s scope list. Example mappings:
| Provider | Scope | Typical Use‑Case |
|---|---|---|
| OpenAI | chat:read | Generate responses only |
| OpenAI | files:write | Upload user‑provided PDFs for summarization |
| Claude | agent:invoke | Run a managed agent that calls internal APIs |
| Cloudflare | workers_ai:invoke | Call a Workers AI model from a worker script |
If a scope is not needed (e.g., no file uploads), omit it. This reduces the impact of a leaked token.
Step 4 – Store Secrets Securely
Never hard‑code keys in source files. Use a secret manager that supports versioning and automatic rotation:
- GitHub Secrets for CI/CD pipelines
- HashiCorp Vault or AWS Secrets Manager for production services
- Cloudflare Workers KV with
CF_ACCESS_CLIENT_IDfor edge‑deployed scripts
When a secret is accessed, retrieve it at runtime, not at build time. This prevents accidental exposure in Docker images or code repositories.
Step 5 – Rotate Keys Regularly
Set a rotation schedule (e.g., every 90 days) and automate the process:
- Create a new credential with the same scopes.
- Update the secret store entry.
- Deploy the change during a low‑traffic window.
- Revoke the old credential after verification.
Both OpenAI and Claude provide API endpoints for key revocation, making automation straightforward.
Step 6 – Monitor and Audit Usage
Implement logging that captures:
- Credential identifier (last 4 characters only)
- Endpoint called (e.g.,
/v1/chat/completions) - Timestamp and request size
- Resulting status code
OpenAI’s usage dashboard and Cloudflare’s analytics API can be queried nightly to flag spikes. Any unexpected call should trigger a review and possibly a key rotation.
Step 7 – Document the Process
Maintain a living document (e.g., a Confluence page) that records:
- All active keys and their scopes
- Owner of each key (team member or service)
- Rotation dates and next‑rotation reminders
- Incident response steps if a key is suspected of being compromised
This documentation satisfies audit requirements and helps new hires understand the security posture.
Putting It All Together
For a typical small‑team workflow that extracts data from PDFs using Cloudflare Workers AI and then posts a summary to Slack via a webhook, you would:
- Create a Cloudflare API token limited to
workers_ai:invokeand the specific zone. - Store the token in Cloudflare Workers KV, accessed only by the worker script.
- Generate a separate Slack webhook secret with
incoming-webhookscope. - Log each invocation with the token’s masked ID.
- Rotate both secrets every 60 days and review logs weekly.
The blast‑radius of a leaked Cloudflare token is now limited to a single worker, not the entire account.
If you need a quick security review of your AI automation keys, AISecAll can run a scoped‑key audit and set up automated rotation pipelines.
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.