On this page
A backend agent is useful when a task needs several context-dependent steps: inspect a support request, retrieve account information, propose a resolution and prepare an update. The dangerous shortcut is giving a model broad credentials and letting its generated plan define what the application is allowed to do.
Treat the model as a planner inside a controlled runtime. Application code owns authority: identity, permissions, budgets, state transitions and side effects. The model can suggest a next step, but the backend must decide whether that step is valid for this user, this workflow and this moment.
Define a narrow workflow contract
Start with one job and an observable completion condition. “Help with operations” is not a specification. “Prepare a refund recommendation for one order, without issuing payment” gives engineers a clear boundary and reviewers something concrete to evaluate.
Write down required inputs, available tools, permitted outputs and stopping conditions. Establish which information can be retrieved and which actions require confirmation. Read and write capabilities should be separate even if the underlying service combines them in one API. A recommendation agent usually does not need mutation privileges.
Prefer deterministic code for predictable steps. If every successful workflow always validates an order ID and fetches its status, do those steps directly. Reserve model decisions for genuine ambiguity rather than making the model rediscover a fixed business process on every request.
Use the AI coding assistant directory to compare implementation aids, not runtime security guarantees. An assistant may help draft adapters and tests, but the same code review and deployment requirements should apply to generated orchestration code as to handwritten code.
Expose small tools with server-owned permissions
Each tool needs a typed input schema, a documented effect and a clear error contract. Avoid generic shell access, unrestricted SQL or arbitrary URL fetching when a narrow operation will work. Least privilege reduces both accidental damage and the impact of malicious instructions hidden in retrieved content.
Derive tenant and user identity from the authenticated execution context. Never trust a model-supplied tenant ID as permission to read another account. Validate resource ownership inside each tool handler, even when an earlier step already checked it; state and permissions can change during a long-running job.
Use the regex tester with synthetic values to check a bounded identifier pattern before adding it to a tool schema. Pattern matching can reject malformed order references, but it cannot prove ownership or authorisation. Keep those checks in trusted application logic.
The Model Context Protocol documentation explains a standard approach to connecting models and tools. A standard interface does not remove your responsibility for permissions, network restrictions or safe handling of tool responses. Treat externally supplied tool descriptions as integration inputs to review, not automatic authority.
Persist state outside the model conversation
A conversation history is not a reliable job database. Store workflow status, completed operations, attempt counts, approval records and output references in durable storage. The model context can be reconstructed from that state without becoming its sole source of truth.
Define explicit state transitions such as queued, running, awaiting approval, completed, failed and cancelled. Reject transitions that do not make sense. A late model response must not move a cancelled workflow back into execution or approve an operation that has already expired.
A small runner contract makes the boundary visible:
Load the job and its authenticated execution scope.
Check cancellation, deadline and remaining step budget.
Ask the model for a proposed typed action.
Validate schema, permissions and allowed state transition.
Require approval for protected side effects.
Execute with an operation ID and persist the result.
Stop on completion, exhaustion or an unresolved error.Use idempotency keys for consequential operations. A timeout does not prove that an external write failed; retrying blindly can issue two refunds or send duplicate emails. Persist an operation intent before dispatch and reconcile ambiguous results using the destination service's guarantees where available.
Bound execution and isolate untrusted content
Set limits for total runtime, tool calls, model requests, output size and cost. A step limit alone is insufficient if one tool can return an enormous document or block indefinitely. Apply timeouts and size bounds at every network boundary, not just around the top-level workflow.
Treat documents, web pages and tool responses as untrusted data. They may contain instructions designed to redirect the model. The OWASP LLM security project is a useful reference for prompt injection and excessive agency. Instruction hierarchy helps, but permission checks must remain independent of model compliance.
Restrict outbound destinations and defend against server-side request forgery when tools access URLs. Avoid passing raw credentials into model context. Prefer scoped server-side clients and redact sensitive fields before returning tool results to the planner.
The AI API privacy guide helps map the data leaving your infrastructure. Include tool results, traces and review screens in that map. An agent that retrieves private account records and then writes them into a third-party debugging trace has crossed a data boundary even if its final response is harmless.
Make approvals and evaluation operational
An approval screen should show the exact proposed side effect: destination, relevant values, affected resources and reason. Human approval must bind to parameters, not to a vague plan. If the model changes the recipient or amount after approval, require another decision rather than reusing the earlier consent.
Record a compact trace containing state transitions, tool names, timing, error classes and approval references. Avoid logging entire sensitive payloads by default. The OpenTelemetry documentation offers concepts for correlating spans across services, though your team must still choose safe attributes and retention rules.
Test workflows with deterministic fixtures, simulated tool failures and adversarial retrieved text. Verify recovery after a worker restart, duplicate queue delivery, revoked permissions and cancellation during a write. Failure-path tests often reveal more operational risk than another successful demo conversation.
Evaluate task completion alongside unauthorised attempts, duplicated side effects, review burden and cost per completed job. The provider selection guide can help structure comparative tests, but changing models should still trigger workflow regression checks. Roll out first in recommendation-only mode, then enable narrowly scoped writes when observed evidence supports them.
Key takeaways
- Keep identity, permissions and workflow state outside model authority.
- Expose narrow tools and validate ownership on every operation.
- Persist progress and make consequential retries idempotent.
- Bound time, cost, context size and tool access independently.
- Bind approvals to exact actions and test recovery before enabling writes.
Reach designers & developers at work
Put your product next to the tools people use every day. One sponsor per placement, no tracking pixels.
Become a sponsor