Neural Mastery
← Practice
Tool Description Budget Fitting for Context Windowmedium

Tool Description Budget Fitting for Context Window

medium Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement tool-description context-budget fitting, deciding how many of an agent's available tool descriptions actually fit in its system prompt.

Task

Implement fit_tool_budget(tools, token_budget). tools is a list of {"name": str, "priority": float, "tokens": int}, sorted here by you in DESCENDING priority. Greedily include tools in that order while the running token total stays <= token_budget. Return the included tool names in the order they were included.

Function Signature

fit_tool_budget(tools: list[dict], token_budget: int) -> list[str]

Examples

Example 1: all fit
Input: tools = [{'name': 'a', 'priority': 0.9, 'tokens': 50}, {'name': 'b', 'priority': 0.5, 'tokens': 50}] token_budget = 200
Output: ['a', 'b']
Example 2: budget forces exclusion of low priority
Input: tools = [{'name': 'a', 'priority': 0.9, 'tokens': 100}, {'name': 'b', 'priority': 0.1, 'tokens': 100}] token_budget = 100
Output: ['a']
Example 3: zero budget includes nothing
Input: tools = [{'name': 'a', 'priority': 1, 'tokens': 1}] token_budget = 0
Output: []

Constraints

  • token_budget >= 0
Python3Saved ✓
def fit_tool_budget(tools, token_budget):
# Your implementation here
pass

Case 1: all fit
Input: tools = [{'name': 'a', 'priority': 0.9, 'tokens': 50}, {'name': 'b', 'priority': 0.5, 'tokens': 50}] token_budget = 200
Expected: ['a', 'b']
Case 2: budget forces exclusion of low priority
Input: tools = [{'name': 'a', 'priority': 0.9, 'tokens': 100}, {'name': 'b', 'priority': 0.1, 'tokens': 100}] token_budget = 100
Expected: ['a']
Case 3: zero budget includes nothing
Input: tools = [{'name': 'a', 'priority': 1, 'tokens': 1}] token_budget = 0
Expected: []