Deadline Remaining Before a Tool Call
Problem Statement
An agent has an overall deadline. Before each tool call, compute how much time is left, and whether there is enough to attempt a call that needs cost seconds.
Background
Given a deadline (absolute seconds), the current time now, and an estimated cost, the remaining budget is max(deadline - now, 0). A call should proceed only if remaining >= cost.
Your Task
Implement:
def deadline_check(deadline, now, cost):
Return a dict with "remaining" (float, clamped at 0) and "proceed" (bool).
Input Format
- deadline (float), now (float), cost (float).
Output Format
- A dict {"remaining": float, "proceed": bool}.
Sample
print(deadline_check(100.0, 90.0, 5.0))
Output:
{'remaining': 10.0, 'proceed': True}
Example:
print(deadline_check(100.0, 90.0, 5.0))
{'remaining': 10.0, 'proceed': True}- Calculate the raw time difference between the absolute deadline and the current time to determine the theoretical budget: 100.0−90.0=10.0.
- Clamp this value at zero to ensure the remaining time is non-negative, which is necessary because negative time is not a valid budget: max(10.0,0.0)=10.0.
- Compare the calculated remaining time against the estimated cost of the tool call to decide if it is safe to proceed: 10.0≥5.0 evaluates to
True. - Combine these results into the required dictionary structure with the keys
"remaining"and"proceed". - The final output is
{'remaining': 10.0, 'proceed': True}
Constraints:
remaining = max(deadline - now, 0).proceed = remaining >= cost.- Return remaining as a float.
1. Background Knowledge
This problem models a fundamental concept in AI agent reliability: resource budgeting under time constraints. In production systems, agents must make tool calls (API requests, database queries, LLM invocations) within strict latency budgets. If an agent attempts a call that will exceed its remaining time, the entire operation may fail or timeout, wasting resources and potentially corrupting state. The "deadline" represents an absolute timestamp (e.g., Unix epoch seconds), while now is the current system time. The difference between them gives the remaining budget.
The key mathematical operation here is clamping. When now exceeds deadline, the raw difference deadline - now becomes negative. However, a negative time budget is physically meaningless—you cannot have "-5 seconds left." Therefore, we clamp the value to zero using max(value, 0). This ensures the remaining field always represents a valid, non-negative duration.
The proceed decision is a simple threshold comparison: the agent should only attempt the call if the remaining budget is sufficient to cover the estimated cost. This is a conservative safety check. In real systems, you might also add a safety margin (e.g., require remaining >= cost + buffer), but for this problem, the strict inequality remaining >= cost is the rule.
2. Algorithm Approach
This is a direct computation problem with no loops or complex data structures. The approach follows a simple linear sequence:
- Compute the raw time difference: deadline - now.
- Clamp the result to zero: max(raw_diff, 0.0).
- Compare the clamped remaining time against cost to determine proceed.
- Package both values into a dictionary.
The pattern is guard clause logic: first ensure the input is valid (non-negative), then make the decision based on that validated value.
3. Step-by-Step Strategy
- Step 1: Calculate raw remaining time Subtract now from deadline. This gives the time left, which may be negative if the deadline has passed.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.