Designing Safe Business API Calls for AI Agents: Parameter Validation, Permissions, Idempotency, and Error Handling
When an AI agent graduates from "chat only" to "actually doing things," the risk profile escalates too. Letting a large model call real business APIs — order lookup, logistics tracking, refund requests — means a wrong parameter, an overstepped permission, or a duplicated request no longer produces a wrong sentence but real financial loss. I have seen teams eagerly wire a dozen tools into their agent without ever answering one question: why should a model-generated call request be executable as-is?
This is the third post in an experience-sharing series for AI customer service product managers and developers, drawn from hands-on practice as an AI product manager at a Southeast Asian cross-border e-commerce platform. In this post I want to share how to design tool definitions, parameter validation, permission control, idempotency, and error handling so that model-generated requests to business APIs such as order lookup, logistics tracking, refund requests, and after-sales tickets execute safely and accurately.

Three Key Points in Safe Tool Calling
- Engineering maturity of Function Calling / Tool Use: not just registering tools, but designing the tool system so the model is "unlikely to make mistakes."
- Security awareness: least privilege, tiered control of high-risk operations — the model must never become a bypass around the permission system.
- Distributed systems fundamentals: idempotency, error handling, and audit logs matter even more in AI scenarios, because models retry, hallucinate, and behave unpredictably.
The Overall Design
1. Tool definitions: make it clear "when to call and how"
- Define each tool with a structured schema (JSON Schema): name, purpose description, parameter types and required fields, return structure, error codes. The clearer the description, the lower the mis-invocation rate.
- Organize tools by business action (query order, query logistics, request refund, create ticket), with query tools and operation tools strictly separated in naming and description, so the model semantically distinguishes "read" from "write."
- Spell out preconditions and constraints in the description, e.g., "order status must be queried and refundable before a refund," pushing business rules down into the tool layer.
2. Parameter validation: model output is an "intent," not a "command"
This is the point I most want to emphasize. A model-generated call request represents only call intent and must pass a deterministic validation layer before execution:
- Parameter types, required fields, value ranges, formats (e.g., order-number rules), business rules (refund amount must not exceed the refundable amount);
- Critical parameters (order number, user identity) must never be freely generated by the model — inject them from session context and login state, eliminating at the source the risk of the model hallucinating someone else's order number;
- On validation failure, return the error to the model and guide it to clarify with the user, not to blindly retry.
3. Permission control: least privilege + tiered control + full audit trail
- Follow the principle of least privilege: each session gets a restricted token based on the logged-in user's identity, able to access only that user's own orders and tickets.
- High-risk operations (refunds, order cancellation, after-sales modification) use tiered policies: auto-execute within amount thresholds; above thresholds, require user confirmation or human review.
- Log everything: who, when, which tool, parameters and results — all auditable.
4. Idempotency: guard against duplicated execution losses
Generate a unique request_id for every write operation (e.g., session ID + message ID + action), and deduplicate by idempotency key on the business API side. Model retries, user page refreshes, and network timeout resends should never cause duplicate refunds or duplicate tickets. In AI scenarios idempotency is not optional — models retry far more often than humans mis-click.
5. Error handling: separate retryable from non-retryable
- Retryable errors (timeout, rate limiting): retry with exponential backoff and a cap;
- Non-retryable errors (invalid parameters, insufficient permission): feed the structured error back to the model to decide whether to clarify or give up;
- Timeout and degradation: when an API is unavailable, honestly tell the user to try later or hand off to a human — never let the model fabricate a "query succeeded" result;
- Result verification: after the business API returns, verify the result matches expectations before replying to the user; report failures honestly.
How This Maps to Weiyu
The Weiyu open-source customer service system (GitHub: https://github.com/bytedesk/bytedesk , website: https://www.weiyuai.cn ) provides AI Agent tool-calling (Function Calling) capabilities, letting you wrap business systems as tools for the agent; it also has a complete agent role and permission system, with controllable human-AI boundaries and handoff timing in collaborative workflows — write-type requests can be designed to require human confirmation before execution; full conversation records and operation logs support auditing. When integrating high-risk APIs such as refunds in production, we recommend layering the idempotency keys and tiered confirmation strategies from this post on top of Weiyu.
Key Takeaways
- Foolproof tool definitions: structured schema + read/write-separated naming + preconditions written into descriptions.
- Model output is intent, not command: a deterministic validation layer is mandatory; critical parameters come from login state, not the model.
- Least privilege is the baseline; tiered control, secondary confirmation, and full logging for high-risk operations.
- Idempotency keys prevent duplicate execution — the fuse against financial loss when AI calls write APIs.
- Classify errors: retryable ones get backoff, non-retryable ones get honest feedback; never fabricate results.
