Connect Application Insights to the Foundry project and use the Agent Monitoring Dashboard as the primary source for historical per-agent token usage; for agents using the azure-ai-agents thread/run API, also read run.usage after each run completes.
Historical dashboard data
In Microsoft Foundry (new):
- Open the Foundry project.
- Select Agents → Traces → Connect.
- Create or connect an Application Insights resource.
- Generate agent traffic and allow a few minutes for ingestion.
- Select Build → select the agent → Monitor.
- Choose the required time range and review the Token usage chart.
The Agent Monitoring Dashboard reads telemetry from the connected Application Insights resource and displays token usage, latency, run success, and evaluation data. The dashboard metrics are currently in preview.^1^
For access:
- The user querying telemetry needs access to Application Insights.
- Log-based queries require Log Analytics Reader.
- Protected tables additionally require Privileged Monitoring Data Reader.
- Assigning these roles might require an Azure subscription or resource administrator.^2^
Telemetry retention and billing follow the connected Application Insights and Log Analytics configuration.
Fetch usage for an individual run
This option specifically applies when the application uses the documented azure-ai-agents thread/run API. A completed run exposes:
-
usage.prompt_tokens
-
usage.completion_tokens
-
usage.total_tokens
usage is null while a run is queued or in progress, so store the values only after reaching a terminal state.^3^
A simplified Python pattern for a run without client-handled function tools is:
import time
run = agents_client.runs.create(
thread_id=thread.id,
agent_id=agent.id,
)
deadline = time.monotonic() + 300
while run.status in ("queued", "in_progress"):
if time.monotonic() >= deadline:
raise TimeoutError("Agent run did not finish within 5 minutes")
time.sleep(1)
run = agents_client.runs.get(thread_id=thread.id, run_id=run.id)
if run.status != "completed":
raise RuntimeError(f"Run ended with status: {run.status}")
usage = run.usage
print(usage.prompt_tokens, usage.completion_tokens, usage.total_tokens)
The SDK documents runs.get for retrieving the updated run. If a run can enter requires_action, do not leave it polling: execute the requested tool calls and submit their outputs, or use create_and_process with an appropriate ToolSet.^4^
Persist a row such as the following for each completed run:
timestamp, agent_id, run_id, prompt_tokens, completion_tokens, total_tokens
This provides immediate, exact run-level data for the application dashboard, while Application Insights supplies historical and operational views.
Custom agents, hosted agents, or the Responses API
Do not assume the azure-ai-agents run.usage model applies to every Foundry agent type. For custom LangChain/LangGraph agents, instrument the application with OpenTelemetry and record:
-
gen_ai.agent.id or gen_ai.agent.name
-
gen_ai.usage.input_tokens
-
gen_ai.usage.output_tokens
These attributes allow token totals to be grouped by agent in Application Insights.^5^
Before writing KQL, open Application Insights → Logs, inspect a recent trace, and identify the table and attribute representation used by that agent type and instrumentation. The exact tables and columns can differ. After validating the query, a custom dashboard can retrieve results through Azure CLI, PowerShell, the Log Analytics REST API, or Azure Monitor Query client libraries.^6^
There is no single aggregate endpoint identified here that can safely be recommended for every Foundry agent type. The supported retrieval path therefore depends on whether the implementation uses the thread/run API, a hosted agent, the Responses API, or a custom framework.
References
- Monitor agents with the Agent Monitoring Dashboard
- Set up tracing in Microsoft Foundry
- RunCompletionUsage Class-Attributes
- Azure AI Agents client library for Python - version 1.1.0
- Trace LangChain and LangGraph apps with Microsoft Foundry and Azure Monitor
- Azure Monitor Logs overview