Lesson 7 of 7 Cost and Capacity Optimization

Monitoring and Controlling Agent Resource Consumption

This lesson explains how to monitor agent resource consumption, which varies widely because agents decide how many model and tool calls a task needs.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify what agents consume.
  • Monitor per-session consumption.
  • Control consumption with iteration limits and token budgets.

What agents consume

Each agent task consumes model tokens for every reasoning step (with the growing context re-sent each time), tool calls that may invoke Lambda functions, APIs or databases, runtime compute (AgentCore Runtime bills active CPU and memory per session, so time spent waiting for model responses uses little CPU), memory operations and storage, and gateway calls. Multi-agent systems multiply these.

Select all that apply

What an agent session costs

Exam skills 4.2.8

Identify what an agent session consumes.

Log in to try this activity Interactive activities are free with an account.

Monitoring consumption

AgentCore Observability and CloudWatch generative AI observability report per-session tokens, model and tool call counts, latency and errors. Useful derived metrics are tokens per task, iterations per session, tool calls per task, session duration and cost per completed task. Distribution views matter more than averages: a few runaway sessions can dominate cost.

Controlling consumption

Set maximum iterations, session time limits and token budgets per task; trim or summarise context between steps; give tools precise descriptions so the agent picks the right one first; use smaller models for simple sub-tasks; cache tool results; and alarm on outliers. The following table summarises the patterns.

PatternSignalControl
Runaway loopsIterations per session spikesMaximum iterations, loop detection
Context growthTokens per step rising within sessionsSummarize or trim context
Tool thrashingMany tool calls per taskBetter tool descriptions, caching
Expensive model for simple stepsHigh cost per taskRoute sub-tasks to smaller models
Long idle sessionsLong session durationSession time limits

The following diagram shows the distribution of tokens per session for an agent, including a long tail caused by repeated tool calls.

The distribution of tokens per agent session. Most sessions sit near the median of 12,000 tokens, but a long tail reaches 400,000 at the 99th percentile: the agent kept calling a search tool with reworded queries when no result was found. A limit of three searches per task, asking the user after an empty result and an alarm above 100,000 tokens bring cost back down.
The scenario is illustrative.
Match the pairs

Control agent spend

Exam skills 4.2.8

Match each agent cost problem with a control.

Log in to try this activity Interactive activities are free with an account.
Worked example

A few expensive sessions

An agent's monthly cost doubles while task volume is flat. The median session uses 12,000 tokens, but the 99th percentile uses 400,000.

Traces of the expensive sessions show the agent repeatedly calling a search tool with slightly reworded queries when no result is found. A maximum of three searches per task, an instruction to ask the user for clarification after an empty result and an alarm on sessions above 100,000 tokens bring cost back down. Looking only at average tokens per session would have hidden the outliers that caused the increase.

Check yourself

Why look at percentiles rather than averages for agent cost?

Show answer

A few runaway sessions can dominate cost while the average looks normal.

Exam Tips

Remember for the exam

  • Monitor tokens, iterations and tool calls per task, not just per request.
  • Look at percentiles; a few runaway sessions can dominate cost.
  • Cap iterations, session duration and token budgets.
  • AgentCore Runtime bills active CPU and memory consumption.
  • Route simple sub-tasks to smaller models.