AWS Builder Center
The AI Agent That Knows When NOT to Think: Building a Cost-Aware AI Router on AWS

The AI Agent That Knows When NOT to Think: Building a Cost-Aware AI Router on AWS

What if an AI application could decide when to answer directly, retrieve evidence, call a tool, or ask for clarification? Explore a practical architecture for building cost-aware, evidence-driven AI agents with Amazon Bedrock, Python, and AWS services, including routing logic, safety controls, and reproducible benchmarks.

The AI Agent That Knows When NOT to Think

Building a Cost-Aware, Evidence-Driven AI Router on AWS

1. Introduction

Not every user request needs a powerful AI model, a knowledge retrieval pipeline, or an autonomous agent.
Consider these four requests:
  • “Explain what an API is.” — A direct answer may be sufficient.
  • “Summarize our company’s internal deployment guide.” — The system needs relevant source material.
  • “Check the current status of my order.” — The system needs an authorized tool call.
  • “Book the meeting for the best time.” — The system may need clarification before taking action.
Treating all four requests identically can increase latency, cost, and the risk of incorrect responses.
This leads to an important design question:
What if an AI application could decide when to answer directly, when to retrieve evidence, when to use a tool, and when to ask for clarification?
In this article, we explore a practical architecture for an adaptive AI router using AWS services. We will examine its routing logic, security boundaries, observability, and a reproducible evaluation plan.
The goal is not to build an agent that performs the most reasoning. It is to build one that performs the right amount of reasoning for each request.

2. The Problem: One Workflow Does Not Fit Every Request

A common implementation sends every request through the same model and tool pipeline.
That approach is straightforward, but it can introduce unnecessary work.
ChallengeWhy it mattersPossible consequence
Overusing powerful modelsSimple requests receive more processing than they needHigher inference cost
Retrieving documents unnecessarilyRetrieval adds extra processing stepsIncreased latency
Calling tools without needEvery tool introduces another dependencyMore failure points
Answering without evidenceThe model may lack the information requiredUnsupported claims
Guessing when information is missingThe system hides uncertaintyPoor user trust
Logging too littleFailures become difficult to diagnoseSlower debugging
A better design evaluates the requirements of each request before selecting an execution path.
The router should not merely ask, “How confident is the model?”
It should ask:
  • Does this request require current or external information?
  • Does it require private or source-specific evidence?
  • Does it require an action?
  • Is the request sufficiently clear?
  • Is the requested action authorized?
  • Can the system answer safely with the available information?
These questions turn routing into an explicit engineering decision rather than an uncontrolled model behavior.

3. Proposed Architecture

The proposed system uses a lightweight routing layer to select an appropriate workflow.
The architecture separates request handling, policy enforcement, execution, and observability.
ComponentAWS serviceResponsibility
Application entry pointAmazon API GatewayAccept requests and apply API-level controls
Routing and policy logicAWS LambdaClassify requests, enforce policies, and select a route
Foundation modelAmazon BedrockGenerate responses using a suitable model
Knowledge retrievalAmazon Bedrock Knowledge Bases and Amazon S3Retrieve relevant source material
Controlled actionsAllow-listed Lambda functions or approved APIsExecute permitted operations
MonitoringAmazon CloudWatchCapture logs, metrics, and operational failures
PermissionsAWS Identity and Access Management (IAM)Apply least-privilege access controls
This is a proposed reference architecture, not a claim that a deployed production system has already been built or benchmarked.
The exact services can be adjusted to match the application. For example, a project that does not need document retrieval can omit the knowledge base component.

Why separate routing from execution?

Keeping routing separate makes the system easier to test and maintain.
The routing layer determines what should happen. The execution layer performs the selected operation. The policy layer checks whether that operation is allowed.
This separation helps prevent the language model from becoming the only authority responsible for choosing and executing actions.

4. How the Router Makes a Decision

The router can select one of four primary paths.
RouteWhen to use itExpected behavior
Direct answerA clear, low-risk request that does not require external evidenceAnswer with an appropriate model
RetrievalThe answer depends on documents or specific knowledge sourcesRetrieve evidence and generate a grounded response
Tool executionThe request requires a supported external action or live dataCheck authorization, validate inputs, and invoke an approved tool
Clarification or abstentionThe request is ambiguous, unsafe, unsupported, or lacks necessary evidenceAsk a question or explain the limitation

Routing should be based on requirements

A simplistic router might classify a request based only on keywords. That can be useful for a first prototype, but keywords alone are not enough for a robust application.
For example, the word “weather” might indicate a need for current information, while “explain how weather forecasting works” may only require a direct explanation.
A more capable router can combine:
  1. Deterministic rules for known requirements and security constraints.
  2. Structured classification for identifying intent and required capabilities.
  3. Policy checks before any action or access to protected data.
  4. Fallback behavior when the request cannot be classified safely.
The classification result should be treated as a routing proposal—not as authorization to perform an action.

5. Implementation: Start With Explicit Routing Rules

For an initial prototype, begin with deterministic routing rules. This makes the behavior easier to inspect and test before introducing more complex classification.
The screenshot illustrates an example route policy with four outcomes: direct, retrieval, tool, and clarification.
In a real implementation, the router would receive structured signals derived from the request and application context. These signals would then be evaluated against the routing policy.
A practical implementation should also define:
  • Which routes are available for each request type.
  • What evidence is required before answering.
  • Which tool actions are permitted.
  • How missing or conflicting information is handled.
  • What happens if a dependency fails or times out.

Important design principle

Routing and authorization are different decisions.
A classifier may identify that a request needs an order-status tool. That does not mean the user is allowed to access every order.
The application must independently verify identity, permissions, input validity, and the scope of the requested action.

6. Secure Tool Execution

Tool calling is one of the most valuable capabilities of an AI agent, but it also introduces additional risks.
A model-generated tool name or argument should never be trusted automatically.
The code snapshot demonstrates an allow-list and authorization check before execution. It is illustrative; a production implementation must also validate arguments, enforce resource-level access, handle errors, and apply timeouts.
A secure tool execution sequence should follow this order:
  1. Identify the required tool.
  2. Verify that the tool is on the approved allow-list.
  3. Authenticate the user and authorize the specific operation.
  4. Validate the arguments against a defined schema.
  5. Execute the operation with least-privilege permissions.
  6. Record the outcome and return only permitted information.
RiskRecommended control
Unauthorized tool callAllow-list tools and enforce authorization
Invalid argumentsSchema validation and input constraints
Excessive permissionsLeast-privilege IAM roles
Repeated or expensive callsBudgets, rate limits, and call limits
Destructive actionExplicit confirmation and additional policy checks
Sensitive information leakageData filtering and access-aware responses
For high-impact actions, such as deleting resources or making financial changes, the application should require appropriate confirmation and authorization rather than relying on the model’s judgment alone.

7. Retrieval and Evidence-Grounded Answers

Some questions cannot be answered reliably from a model’s general knowledge alone.
Examples include questions about:
  • Internal technical documentation.
  • A company’s deployment procedures.
  • A product-specific knowledge base.
  • Documents that may have changed since the model was trained.
For these requests, the router can select a retrieval workflow using Amazon Bedrock Knowledge Bases and a supported data source, such as Amazon S3.
The workflow is:
  1. Retrieve relevant passages from the permitted knowledge source.
  2. Check whether the retrieved material is relevant to the question.
  3. Generate an answer using the available evidence.
  4. Provide citations or source references when supported by the application.
  5. Abstain or ask for clarification if the evidence is insufficient.
Retrieval does not automatically guarantee correctness. A system can retrieve irrelevant documents, miss critical context, or generate claims that the retrieved material does not support.
Evaluation should therefore measure both retrieval quality and answer groundedness.

8. When the Correct Answer Is “I Don’t Know”

A reliable AI system must distinguish between a missing answer and an answer that can be safely inferred.
Consider these situations:
SituationRecommended response
The request is ambiguousAsk a targeted clarification question
Required information is unavailableExplain what information is missing
Retrieved sources conflictDescribe the conflict or request verification
The user lacks permissionDecline the restricted action
A tool failsReport the failure without inventing a result
Evidence does not support a conclusionState the limitation instead of guessing
Abstention is not simply refusing to answer. It is a deliberate outcome when the available evidence, permissions, or request details are insufficient.
The system should also distinguish between uncertainty about factual content and uncertainty about whether an action is authorized. The latter must be handled through policy checks, not model confidence.

9. Benchmarking the Router

An adaptive router should be evaluated against simpler alternatives to determine whether the additional complexity is worthwhile.
Compare three configurations:
ConfigurationExecution strategyWhat it helps evaluate
Single-model baselineSend each request to one selected modelBaseline quality, latency, and cost
Retrieval-firstUse retrieval for most or all requestsEffect of retrieval overhead and evidence availability
Adaptive routerSelect a route based on request requirementsWhether routing improves the overall trade-off

Build a reproducible evaluation dataset

A useful starting point is a test set of approximately 100 requests, distributed across four categories:
CategorySuggested test casesExample
Direct answers25Explain the difference between HTTP and HTTPS
Retrieval questions25Summarize a specific section of a supplied document
Tool requests25Retrieve an authorized order status using a mock tool
Clarification or abstention25Respond to an ambiguous request with missing information
These counts are a proposed evaluation plan, not measured results. The dataset should include expected outcomes, relevant evidence where applicable, and a consistent scoring method.
Use the same test cases and comparable model settings across configurations. Record failures as well as successful requests.

Metrics to measure

MetricWhat it measures
Task success rateWhether the system fulfilled the request correctly
Groundedness or citation pass rateWhether evidence-based claims are supported by the retrieved sources
Average latencyTypical end-to-end response time
p95 latencyResponse time for the slowest 5% of requests
Cost per 100 requestsEstimated model and supporting-service cost
Unnecessary tool-call rateHow often a tool is called when the task does not require it
Unsafe-action rateWhether the system attempts or performs prohibited actions
Appropriate abstention rateWhether it declines or clarifies when required
Replace the TBD values in the image with your measured results. Document the test date, model configuration, dataset, and scoring rules so readers can understand how the results were produced.

Estimate the cost

For model inference, an approximate calculation is:
Model cost per request
= (input tokens × input price per token)
  • (output tokens × output price per token)
For the full application, also account for applicable retrieval, storage, Lambda, API Gateway, logging, and other service charges.
Use the pricing applicable to the selected AWS Region, model, and service configuration. Actual costs depend on usage and pricing changes.
Do not assume that an adaptive router will always be cheaper. Classification itself has a cost, and retrieval or tool execution may be necessary for correctness. The goal is to measure whether the complete design improves the quality-cost-latency trade-off.

10. Observability: Make Routing Decisions Explainable

When a response is incorrect or unexpectedly expensive, developers need to understand which route was selected and why.
The example demonstrates structured logging for a request ID, selected route, routing reason, latency, token usage, tool calls, and outcome.
A production implementation can send appropriate logs and metrics to Amazon CloudWatch.
Recommended fields include:
FieldPurpose
Request IDCorrelate events across components
RouteIdentify the execution path
Route reasonExplain the policy or requirement that selected the path
LatencyIdentify performance bottlenecks
Token usageSupport model cost analysis
Tool-call countDetect unnecessary or repeated tool execution
OutcomeRecord success, failure, clarification, or abstention
Avoid logging secrets, credentials, raw sensitive documents, or unnecessary personal data. Use access controls, retention policies, and redaction appropriate to the application.
Observability should make system behavior understandable without creating a new privacy or security risk.

11. Common Failure Modes and Mitigations

Failure modeWhy it happensMitigation
Direct route chosen for a live-data requestThe router misses a requirementAdd explicit requirement detection and targeted tests
Retrieval returns irrelevant contentPoor matching or incomplete sourcesEvaluate retrieval quality and relevance thresholds
Tool called without permissionClassification is mistaken for authorizationEnforce independent authorization checks
Router adds more latency than it savesClassification is unnecessarily expensiveKeep simple rules deterministic and measure overhead
Model invents unsupported detailsEvidence is missing or ignoredRequire evidence for grounded claims and test abstention
Cost increases unexpectedlyRetries, long outputs, or excessive tool callsSet budgets, limits, timeouts, and alerts
No routing strategy eliminates every failure. The objective is to make failures observable, bounded, and recoverable.

12. A Practical Implementation Roadmap

Build the project incrementally instead of starting with a fully autonomous agent.
PhaseWorkDeliverable
1. Define the taskChoose the supported request types and expected outcomesTest specification
2. Build a baselineImplement a single-model workflowBaseline application
3. Add routingIntroduce explicit route selectionAdaptive router
4. Add retrievalConnect a supported knowledge sourceEvidence-based responses
5. Add a mock toolImplement an authorized, non-destructive toolControlled tool workflow
6. Add observabilityRecord latency, route, tokens, and outcomesOperational metrics
7. EvaluateRun the same dataset across all configurationsCompleted comparison table
8. Harden securityTest permissions, injection attempts, failures, and limitsSecurity test results
Start locally with mocked model and tool interfaces if you want to validate the routing logic before provisioning AWS resources. Add real services only when each stage is testable.

13. Security and Responsible Design

The router should not be treated as a security boundary by itself.
A production implementation should address:
  • Prompt injection: Treat retrieved documents and external content as untrusted input.
  • Least privilege: Give each AWS component only the permissions it requires.
  • Tool authorization: Verify permissions independently of model-generated decisions.
  • Input validation: Validate tool arguments and constrain accepted values.
  • Cost controls: Apply quotas, rate limits, timeouts, and monitoring.
  • Data protection: Redact sensitive values and restrict log access.
  • Human confirmation: Require appropriate approval for consequential actions.
  • Failure handling: Ensure that timeouts and service errors do not produce fabricated success messages.
These controls are essential if the prototype evolves into a real application.

14. Conclusion

Building an AI agent is not just about choosing a model or connecting tools. It is about deciding which capabilities a request actually needs—and enforcing that decision safely.
An adaptive AI router provides a structured way to make that choice. Direct answers can remain lightweight, retrieval can provide evidence, tools can perform authorized operations, and clarification can prevent the system from guessing when important information is missing.
The key engineering principle is simple:
Use the minimum workflow that can satisfy the request reliably, and measure whether it actually improves quality, latency, and cost.
The next step is to implement the baseline, add the routing layer, and publish the measured comparison. That turns the idea from an architecture proposal into a reproducible engineering project.

References and Further Reading

Suggested tags: Artificial Intelligence, Amazon Bedrock, Generative AI, AI Agents, AWS Architecture
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article