Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Control Tool Access & Parallelism in Multi-Agent AI

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When multiple AI agents work together, they share a pool of tools like web search, APIs, or code execution. Without guardrails, one runaway agent can burn through your entire API budget in minutes or swamp a service with parallel requests. This guide shows you how to set spending limits and control parallelism in your multi-agent workflow, so your system stays fast, predictable, and cost-effective.

What You'll Need

  • An orchestration framework or codebase where you define agents (e.g., LangChain, CrewAI, AutoGen, or a custom Python script).
  • Access to the tool APIs you're using (OpenAI, Anthropic, SerpAPI, etc.) and the ability to set API keys or tokens.
  • Basic familiarity with configuration files (JSON, YAML) or environment variables.
  • If you want per-request cost tracking, a way to log tool calls (e.g., a database or log file).

Step-by-Step

  1. List every tool your agents can call. Write down each tool (search, database query, file write, external API) and which agents have access to it. This is your inventory.
  2. Assign a cost per tool call. For paid APIs, use the published price per request or per token. For internal tools, estimate a cost based on compute time or simply assign a weight (e.g., 1 unit per call).
  3. Set a global spending cap. Decide on a maximum total spend per workflow run, per hour, or per day. Put this in a central config file or environment variable, e.g., MAX_TOTAL_SPEND=100 (in your chosen unit).
  4. Set per-agent budgets. Give each agent its own limit, so one agent can't consume the whole budget. For example, a research agent might get 60% of the total, while a summarizer gets 20%.
  5. Add a cost tracking layer. Before every tool call, your code should check the current spend against the limit. If the limit is reached, block the call and return a message like “Tool budget exceeded.”
  6. Implement a rate limiter. Use a token bucket or sliding window to cap requests per second or per minute. This prevents a burst of parallel calls from overwhelming the tool provider.
  7. Control parallelism per agent. In your orchestrator, set a maximum number of concurrent tool calls per agent. For example, max_parallel_calls=2. This is separate from the number of agents running at once.
  8. Set a global parallelism limit. Cap the total number of simultaneous tool calls across all agents. This is crucial if you have many agents.
  9. Prioritize tools. If some tools are critical, give them a higher priority in the queue. When the budget is low, low-priority tools get blocked first.
  10. Test with dry runs. Run your workflow with a small test budget and low parallelism to see if the limits trigger correctly.
  11. Monitor and log. Log every tool call with its cost and timestamp. Review these logs to adjust limits over time.

Common Mistakes

  • Setting limits only on the API side. If you rely on the API provider's rate limit, you can't control which agent gets the budget. Your own layer is essential.
  • Ignoring token costs. Some tools charge per token, not per request. A single long response can cost more than many short ones.
  • Forgetting retries. If a tool call fails and you retry, that counts as a new spend. Include retry logic in your cost tracking.
  • Hardcoding limits. If you change a limit, you shouldn't have to edit code. Use environment variables or config files.
  • Blocking all calls when budget is low. Instead, allow critical tools and block non-essential ones.

Tips

  • Use a shared in-memory counter or a database for cost tracking, so all agents see the same number.
  • Set a “soft” limit at 80% of your budget to warn you before you hit the hard cap.
  • For parallelism, start with a low number (like 2) and increase gradually. You can always raise it later.
  • If you're using a framework like LangChain, check if it has built-in callbacks for cost tracking. Use them instead of building your own.
  • Consider caching tool results. If two agents ask the same question, return the cached answer instead of making a new API call.

Troubleshooting

  • Agents still exceed the budget. Check if your cost tracking is thread-safe. In a multi-threaded environment, use a lock or atomic operations.
  • Parallelism limit not working. Make sure the limit is applied at the tool-call level, not just at the agent level. If you have 10 agents each with 2 parallel calls, you get 20 total.
  • Tool calls time out. If you're limiting parallelism too aggressively, requests might queue up and time out. Increase the timeout or lower the queue length.
  • Cost tracking is inconsistent. If you're using multiple services, make sure you convert all costs to the same unit (e.g., USD).
  • Budget resets unexpectedly. If you're using a serverless function, the in-memory counter resets on cold starts. Use a persistent store like Redis.

Frequently Asked Questions

Q: Can I set different spending limits for different agents?
Yes, that's the recommended approach. Each agent can have its own budget, and you can also have a global budget that overrides all.

Q: What's the difference between parallelism and rate limiting?
Parallelism is the number of concurrent calls you allow at once. Rate limiting is the maximum number of calls per second. You need both to avoid overwhelming a service.

Q: How do I calculate the cost of a tool call if it's not a paid API?
You can assign a nominal cost (e.g., 1 unit) to represent compute time or internal resource usage. The goal is to have a relative measure.

Q: Should I use a fixed limit or a dynamic one?
Start with a fixed limit. Once you have logs, you can adjust. Dynamic limits based on time of day or workload are advanced and not necessary initially.

Q: What happens when a tool call is blocked due to budget?
The agent should receive a clear error message and be able to continue with a fallback, like using a cheaper tool or skipping that step.

Q: Can I control parallelism per tool, not just per agent?
Yes, if a specific tool has a low rate limit, you can set a separate parallelism cap for it. This is useful for third-party APIs.