Score ADK tool trajectories and final answers
Measure tool choice, arguments, step limits, answer quality, latency and token use, accepting more than one valid trajectory where it makes sense.
By Invicly Tech, , in Google ADK with Python for AI Agents. About 11 minutes to read.
In short
A trajectory is the sequence of tool calls an agent makes on the way to its answer. ADK scores it with tool_trajectory_avg_score, which compares actual calls to expected ones using EXACT, IN_ORDER or ANY_ORDER matching, and scores the final answer with criteria such as response_match_score or an LLM judge. Efficiency numbers for tool calls, model calls, tokens and duration are reported alongside, so you can see cost and speed next to correctness.
Think of it like this: Marking a maths exam: you check the working as well as the final number, and you accept more than one valid method.
Explained simply
An agent can give the right answer for the wrong reason. It might guess a ticket status without looking it up, or call the lookup three times when once would do. That is why you check the path as well as the destination. The path is called the trajectory: the list of tools the agent called, in order, with their arguments.
ADK compares the actual trajectory with the expected one and gives a score from 0 to 1. You choose how strict the match is: exact, in order with extras allowed, or any order. Then it scores the final answer, either by counting shared words with a reference answer or by asking another model to judge whether the meaning matches.
Sometimes several paths are fine. An agent may search the docs before checking a ticket or after; both work. Loose match types or your own scoring function let you accept every valid path while still catching wrong ones.
Correct is not the whole story. ADK also reports how many tools and model calls a turn used, how many tokens it consumed and how long it took. Watch these numbers: they are your cost and speed.
Press Play on a diagram to walk through it one step at a time.
- Same calls, same order
- No extra calls allowed
- Args must match
- Best for fixed workflows
- Expected calls appear in order
- Extra calls in between are fine
- Good when a lookup may repeat
- Still catches wrong ordering
- All expected calls appear
- Order does not matter
- Extra calls are fine
- Good for independent lookups
- 1Efficiency counts
Tool calls, model calls, tokens, seconds; informational
- 2Trajectory score
Deterministic match of names and args
- 3Word overlap
response_match_score, ROUGE-1, no model
- 4LLM judge
Meaning match, rubrics, hallucination check; needs a model
In depth
How tool_trajectory_avg_score works
For each turn, ADK takes the expected tool_uses from the eval case and the tool calls the agent actually made, and compares names and arguments. A turn scores 1 if it matches under the chosen rule and 0 if it does not. The case score is the average over its turns, and it passes if it reaches the threshold. With the default threshold of 1.0, every turn must match.
The match_type setting decides what counts as a match. EXACT, the default, wants the same calls in the same order with nothing extra. IN_ORDER wants the expected calls in order but allows extra calls between them. ANY_ORDER wants all the expected calls to appear in any order, with extras allowed. Arguments still have to match, so normalise inputs in your tools (for example upper-case ticket IDs) rather than loosening the test.
- Score per turn: 1 for a match, 0 for no match
- Case score: average over turns
- Default threshold 1.0, default match_type EXACT
Accepting more than one valid trajectory
Decide what really matters. Usually it is a small set of required calls, for example the agent must look up the ticket before stating its status, and must never call a tool it does not have. Write only the required calls as the expectation and use IN_ORDER or ANY_ORDER.
When the rules are more complex, such as either path A or path B but never both, write a custom metric. A custom metric is a Python function that receives the metric config and the actual and expected invocations and returns an EvaluationResult with an overall score, an overall status and per-invocation results. You register it under custom_metrics in the eval config with the function's import path, and give it a threshold under criteria. Another option is rubric_based_tool_use_quality_v1, where an LLM judge checks yes-or-no rubrics you write, such as the agent looked up the ticket before answering.
- Yes
- EXACT match
- Typical for fixed workflows
- Required calls, extras harmless
- IN_ORDER if order matters
- ANY_ORDER if it does not
- Several distinct valid paths
- Custom metric with an allowed list
- Or a tool-use rubric with an LLM judge
Grading the final answer
response_match_score is ROUGE-1: it counts how many single words the answer shares with the reference. It needs no model, is fast and is repeatable, but it is blunt. A correct paraphrase can score 0.5 while a wrong answer that repeats the right words scores well. The default threshold when you set no criteria is 0.8.
final_response_match_v2 asks an LLM judge whether the answer means the same as the reference, and can sample the judge several times with num_samples. rubric_based_final_response_quality_v1 checks rubrics you write, such as mentions the ticket ID or offers a human handover. hallucinations_v1 checks whether each sentence is supported by the context the agent had. All LLM-judged criteria need model access and cost money, so use them in the live lane, not on every commit.
Bounded execution, latency and tokens
An agent loop must stop. RunConfig has max_llm_calls, which caps model calls per run; the default is 500, far too high for a support agent, so set something like 10. Zero or a negative value means unlimited, which you should not use in production.
ADK reports four efficiency numbers for each invocation: tool_call_count_v1, inference_call_count_v1, token_usage_v1 and invocation_duration_v1. They are always on and never pass or fail a case, and ADK rejects a threshold on them. To enforce a limit, read the numbers from the results in your own test, or count function calls and usage_metadata from the runner's events and assert on them. Latency from a live model varies, so compare medians and the 95th percentile across runs, not single numbers.
- Set RunConfig(max_llm_calls=10) for a small agent
- Track tool calls, model calls, tokens and seconds per turn
- Compare median and p95 over repeated runs
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Score tool selection and arguments~35m
- 2Allow several valid trajectories~35m
- 3Check step limits were respected~35m
- 4Grade the final answer~35m
- 5Track latency and tokens~35m
Key points
- tool_trajectory_avg_score checks tool names and arguments per turn; 1.0 means every turn matched.
- Pick IN_ORDER or ANY_ORDER when several trajectories are valid, or write a custom metric that accepts a list of allowed paths.
- response_match_score is ROUGE-1 word overlap; final_response_match_v2 asks an LLM judge whether the meaning matches.
- Bound execution with RunConfig max_llm_calls (default 500) and check the reported tool and model call counts.
- tool_call_count_v1, inference_call_count_v1, token_usage_v1 and invocation_duration_v1 are reported but never fail a case.
Key terms
Tap a card to flip it. 0/6 known
Examples
{
"criteria": {
"tool_trajectory_avg_score": {"threshold": 1.0, "match_type": "IN_ORDER"},
"response_match_score": 0.7
}
}test_config.json: require the expected calls in order, allow extra calls, and a 0.7 word-overlap answer score.
Loosening a trajectory without hiding bugs
A case expects get_ticket_status(T-1001). In live runs the agent sometimes calls search_docs first to explain what open means, and the EXACT check fails. Step 1: read the traces; the extra call is helpful, not wrong. Step 2: switch the criterion to IN_ORDER so extra calls are allowed but the ticket lookup must still happen. Step 3: remember that IN_ORDER with an empty expectation passes whatever the agent calls, so denied-tool cases need their own check. Add a test that no write tool name appears in any trajectory, and keep the judge on the answer in the live lane.
{
"criteria": {
"tool_trajectory_avg_score": {"threshold": 1.0, "match_type": "IN_ORDER"},
"final_response_match_v2": {
"threshold": 0.8,
"judge_model_options": {"judge_model": "gemini-flash-latest", "num_samples": 3}
}
}
}Scoring a list of allowed paths in plain Python
Some cases have two valid paths and one forbidden one. Step 1: write a helper that takes the actual calls as a list of names and a list of allowed paths. Step 2: return 1.0 if the actual path equals any allowed path and 0.0 otherwise, and always 0.0 if a forbidden tool appears. Step 3: call this helper from a custom metric function, building the name list from each actual invocation's tool calls, and register it under custom_metrics with a threshold of 1.0.
FORBIDDEN = {"close_ticket", "update_ticket"}
def path_score(actual: list[str], allowed: list[list[str]]) -> float:
if FORBIDDEN & set(actual):
return 0.0
return 1.0 if actual in allowed else 0.0
allowed = [["get_ticket_status"], ["search_docs", "get_ticket_status"]]
assert path_score(["search_docs", "get_ticket_status"], allowed) == 1.0
assert path_score(["close_ticket"], allowed) == 0.0
assert path_score([], allowed) == 0.0Quick reference
- tool_trajectory_avg_score: match_type EXACT (default), IN_ORDER or ANY_ORDER
- response_match_score: ROUGE-1 word overlap, default threshold 0.8, no model needed
- final_response_match_v2 and rubric criteria use an LLM judge: live lane only
- Efficiency: tool_call_count_v1, inference_call_count_v1, token_usage_v1, invocation_duration_v1
- Efficiency metrics never fail a case; enforce limits in your own assertions
- RunConfig max_llm_calls default is 500; set a small cap such as 10
- Custom metric: function path under custom_metrics plus a threshold under criteria
Common mistakes
- Using EXACT matching for an agent that may legitimately search twice, then chasing false failures.
- Trusting a single ROUGE score for answer quality: a correct paraphrase can score low and a wrong answer with the right words high.
- Ignoring the efficiency numbers until the bill arrives; a correct agent that makes 12 model calls per turn is still a problem.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write a test_config.json that requires exact tool trajectories and a word-overlap score of at least 0.7.
- Exercise 2medium
Count tool calls and total tokens from a run so a pytest test can assert a budget.
- Exercise 3hard
Design scoring for a case where the agent may answer from docs only, or from docs plus a ticket lookup, but must never call a write tool and must cite a doc ID.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Why does a correct answer fail response_match_score?
- Because ROUGE-1 counts shared words, not meaning. Shorten the reference to the key facts, lower the threshold slightly, or use final_response_match_v2 in the live lane.
- Do I need a Google Cloud project for evaluation?
- Not for trajectory and word-overlap scores. LLM-judged criteria need model access, and some multi-turn and safety criteria need Google Cloud project settings.
- Can I write my own metric?
- Yes. Custom metrics have been supported since ADK Python 1.18: a Python function returning an EvaluationResult, registered under custom_metrics in the eval config.