Find the latency and critical path of an agent's task plan

HardAgentsPro

Why it matters Agent orchestrators use critical paths to estimate response time and decide which tool calls are worth speeding up.

A planner splits a request into tasks with dependencies. If independent tasks run in parallel, how long does the whole plan take, and which chain of tasks decides that time?

Write plan_latency(tasks). tasks maps a task name to (duration, list of dependency names). With unlimited parallelism a task starts when all its dependencies have finished: finish[t] = duration[t] + max(finish of its deps), or just duration[t] with no deps. If the dependencies contain a cycle, return None. Otherwise return (total, path) where total = max finish time and path is the critical path from first task to last: start from the task with the largest finish (alphabetically smallest name on ties), then repeatedly step to its dependency with the largest finish (alphabetically smallest on ties) until a task with no deps; reverse to get start-to-end order.

Signaturedef plan_latency(tasks)

Example

plan_latency({"search": (2, []), "fetch": (3, ["search"]), "calc": (1, ["search"]), "answer": (1, ["fetch", "calc"])}) gives (6, ['search', 'fetch', 'answer']).

Understand the idea

When an agent's plan is a graph of tasks, some tasks can run at the same time and some must wait. The total time is not the sum of durations; it is the length of the longest chain of dependent tasks, called the critical path. Speeding up any task off that chain does not make the answer arrive sooner.

Example: search takes 2. fetch (3) and calc (1) both wait for search, and answer (1) waits for both. fetch finishes at 5, calc at 3, so answer starts at 5 and finishes at 6. The critical path is search, fetch, answer.

Rule: finish(t) = duration(t) + max over deps of finish(dep); total = max finish; follow the latest-finishing dependency backwards for the path.

Agent frameworks that run tool calls in parallel use this to estimate latency and to show which slow tool to optimise first.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: exampleplan_latency({'search': (2, []), 'fetch': (3, ['search']), 'calc': (1, ['search']), 'answer': (1, ['fetch',...
  • Sample 2: parallel rootsplan_latency({'a': (5, []), 'b': (1, []), 'c': (2, ['a', 'b']), 'd': (4, ['b'])})