Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Design Tool APIs That Keep AI Agents Within Authority

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide explains how to design tool APIs (Application Programming Interfaces) for AI agents so that the agents can only perform actions you explicitly allow, and cannot exceed their permissions. You will learn concrete patterns for scoping inputs, validating requests, and layering checks, which you can apply to any API that an AI agent (like a chatbot or automation) calls on behalf of a user.

What You'll Need

  • An existing API endpoint or set of endpoints you want to expose to an AI agent.
  • Access to the server-side code that handles requests to those endpoints.
  • A basic understanding of HTTP methods (GET, POST, DELETE) and JSON.
  • Familiarity with your authentication system (e.g., OAuth, API keys, session tokens).

Step-by-Step

  1. List every action the agent may take. Write down each operation your AI agent can perform via the API. For each, note the exact data it needs (e.g., user ID, item ID, quantity) and the effect it has on your system. This list becomes your "authority contract."
  2. Define the narrowest scope for each action. For every action, decide the minimum set of parameters and the maximum values allowed. For example, if the agent can update a user profile, allow only fields like "name" or "email", not "role" or "balance". Write these limits as explicit rules, not as vague guidelines.
  3. Create a dedicated API endpoint for agent use. Instead of giving the AI agent access to your general user-facing API, build a separate set of endpoints (e.g., /api/agent/action) that only accept requests from the agent. This isolates the agent from the rest of your system and makes auditing easier.
  4. Use a fixed request schema. Define a strict JSON schema for each agent endpoint. The schema should list exactly which fields are allowed, their types, and any constraints (e.g., minimum/maximum length, allowed enum values). Reject any request that does not match the schema with a 400 Bad Request.
  5. Validate every parameter server-side. Never trust the agent to send valid data. On the server, check that each parameter is of the correct type, within range, and belongs to the user context. For example, if the agent can delete a file, verify that the file ID belongs to the authenticated user.
  6. Enforce identity and ownership. The agent must act on behalf of a specific user. Pass the user ID in the request (from your authentication token) and ensure that every action is scoped to that user. For instance, if the agent lists orders, filter by the user's ID; if it updates an order, verify the order belongs to that user.
  7. Add a permission check layer. Even if the agent is acting for a user, the user may not have permission to perform that action. Check the user's role or permissions against the action. For example, only allow "admin" users to delete posts. Return 403 Forbidden if not allowed.
  8. Limit the number of actions per request. If the agent can perform batch operations, cap the batch size (e.g., maximum 100 items). This prevents the agent from accidentally or maliciously affecting too many resources at once.
  9. Use idempotency keys for mutating actions. For POST/PUT/DELETE operations, require a unique idempotency key (a string the agent generates). The server stores the key and returns the same result if the same key is sent again. This prevents duplicate actions if the agent retries a request.
  10. Log every agent call. Record the agent's ID, the user ID, the action, the parameters, and the timestamp. This gives you an audit trail to detect misuse and debug issues.
  11. Test with adversarial examples. Write test cases that try to violate the rules: sending extra fields, out-of-range values, or trying to access another user's data. Ensure the API rejects them all.
  12. Document the agent API clearly. Provide documentation that lists exactly what the agent can do, the schema, and the error codes. This helps developers who build agents to stay within the intended scope.

Common Mistakes

  • Reusing the same API for humans and agents. This exposes all endpoints to the agent, increasing the risk of unintended actions.
  • Relying on the agent to send the correct user ID. The user ID must come from your authentication system, not from the request body, or anyone could impersonate another user.
  • Not validating types or ranges. An agent might send a negative quantity or a string where a number is expected, causing errors or security issues.
  • Allowing the agent to set fields that should be server-controlled. For example, letting the agent set a "created_at" timestamp or a "verified" flag.
  • Forgetting to check ownership. Even if the user is authenticated, the agent might try to access resources that belong to another user.
  • Ignoring rate limits. Without rate limits, an agent could overwhelm your system or perform a denial-of-service attack.

Tips

  • Use allowlists instead of denylists. Explicitly list what the agent can do, rather than trying to block what it cannot. This is safer because unknown actions are denied by default.
  • Keep the API as small as possible. The fewer endpoints and parameters, the easier it is to secure.
  • Return generic error messages. Avoid revealing internal details (e.g., "invalid user ID format") that could help an attacker.
  • Implement a "dry run" mode. Allow the agent to test an action without actually executing it, returning what would happen. This helps catch mistakes before they cause harm.
  • Review logs regularly. Look for unusual patterns, such as many failed attempts or actions outside normal hours.

Troubleshooting

The agent gets a 403 even though the user has permission. Check if the agent's own permissions are set correctly. Some systems require the agent to have a specific role or scope. Also verify that the user ID is being passed correctly from the token.

The agent can still access data it shouldn't. Review your ownership checks. Ensure that every query includes a filter by user ID, and that you are not using a shared database connection that bypasses filters.

Requests fail with a 400 due to schema validation. Check the agent's request against your schema. Look for extra fields, wrong types, or missing required fields. Use a JSON schema validator in your tests to catch mismatches early.

The agent causes duplicate actions. If you didn't implement idempotency keys, add them. If you did, check that the key is being stored and compared correctly, and that the agent is reusing the same key for retries.

Rate limiting is blocking legitimate use. Adjust the limits based on the agent's typical usage. Consider using a separate rate limit pool for the agent API so it doesn't compete with human users.

Frequently Asked Questions

Can I use the same API for both humans and AI agents?

Technically yes, but it's risky. A separate agent API reduces the attack surface and makes it easier to enforce agent-specific rules.

How do I handle agents that need to perform multi-step actions?

Design each step as a separate endpoint, and require the agent to call them in sequence. Use a transaction or a state token to ensure that the steps are completed as a unit.

What if the agent needs to access data from multiple users?

That should be an administrative action. Only allow it if the agent has a special admin scope, and require additional checks like two-factor authentication.

Should I use OAuth scopes for the agent?

Yes, OAuth scopes are a good way to limit what the agent can do. Define scopes like "read:orders" or "write:profile" and assign them to the agent.

How do I prevent the agent from being tricked into doing something harmful?

Even with a well-designed API, an agent can be manipulated via prompt injection. Always validate inputs server-side, and consider adding a confirmation step for irreversible actions.

What is the most important thing to remember?

Never trust the agent. Treat every request as untrusted input and apply the same security checks you would for any external user.