BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Agents8 of 28 in topic

723. Deduplicate repeated tool calls with a canonical cache key

EasyAgents

Why it matters Agent runtimes cache idempotent tool calls such as searches and file reads to cut latency and API cost.

Agents often repeat the same tool call, sometimes with arguments in a different order. Caching by a canonical key saves money and time.

Write dedupe_calls(calls). calls is a list of (name, args) where args is a JSON-serialisable dict. The cache key is name + "|" + json.dumps(args, sort_keys=True, separators=(",", ":")). Walk the calls in order: the first time a key is seen the call is executed; later calls with the same key are cache hits. Return (number of executed calls, list of indices of the cache hits).

Functiondef dedupe_calls(calls)

Example 1

Input: dedupe_calls([("search", {"q": "a", "k": 2}), ("search", {"k": 2, "q": "a"}), ("calc", {"x": 1})])

Output: (2, [1])

Example 2

Input: calls = [('f', {'a': {'y': 1, 'x': 2}}), ('f', {'a': {'x': 2, 'y': 1}}), ('g', {'a': {'x': 2, 'y': 1}}), ('f', {'a': {'x': 2, 'y': 1}})]

Output: (2, [1, 3])

Example 3

Input: calls = [('f', {'n': 1}), ('f', {'n': 1.5}), ('f', {'n': '1'})]

Output: (3, [])

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

Two dicts with the same items in a different order must give the same key.

Hint 2

json.dumps(args, sort_keys=True, separators=(',', ':')).

Hint 3

Include the tool name in the key.

Hint 4

The executed count equals the number of distinct keys.

Common mistakes
  • Not sorting keys.
  • Leaving the tool name out of the key, so different tools collide.
  • Using str(args), which depends on insertion order.
Understand the idea

Large language models repeat themselves: an agent may search for the same thing twice in one task, often writing the arguments in a different order. If a tool is idempotent (same input, same output), we can reuse the first answer. The trick is a canonical key: a string that is identical whenever two calls mean the same thing.

Worked example

search with {"q": "a", "k": 2} and search with {"k": 2, "q": "a"} are the same call. Serialising with sorted keys gives the same string for both, so the second is a cache hit and only 2 of the 3 calls run.

key = name + "|" + json.dumps(args, sort_keys=True, compact separators); execute if new, else return the cached result

Only cache read-only tools; caching a "send email" call would silently drop real actions.

Topics
Agents
Similar questions
  • Parse a ReAct agent's action from model outputEasy
  • Trim an agent's chat history to fit a token budgetEasy
  • Detect a repeating tool-call loopEasy
  • Route a request to a tool by keyword scoreEasy
All Agents problems
Report
723 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import json
​
def dedupe_calls(calls):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS