3 Control Mechanisms to Reduce Multi-Agent Token Costs and Prevent Data Leaks
If you run autonomous agent prototypes for just a few days, you will likely face a shocking situation. Cloud API costs rack up hundreds of dollars simply because a few agents exchanged messages with each other. This happens because agents consume significantly more tokens than single-shot queries due to their characteristic 'perception-reasoning-action-reflection' loop. When you also consider the risk of internal database information or API keys leaking directly to external servers, applying this to production feels distant.
Most cost explosions and security violations can be resolved simply by building proper engineering control lines.
Preventing Infinite Inter-Agent Dialogues and Implementing Soft Landings
Agents exchanging meaningless greetings like "Understood" or "Thank you" is a primary culprit that drains budgets. Relying solely on a framework's default recursion limit settings will cause the server to crash with a 500 error when the limit is exceeded.
To recover responses without service disruption, a 3-stage routing control is required.
- Place a step counter variable inside the framework's state object.
- Execute a router function that checks whether the counter has reached a specified count (e.g., 5 times).
- When the threshold is exceeded, instead of throwing an exception, divert the flow to a fallback node to summarize the results so far and terminate.
System prompts must directly include control statements to prevent meaningless text generation.
`text
[SYSTEM INSTRUCTION: COMMUNICATION PROTOCOL]
DO NOT generate conversational filler, pleasantries, or acknowledgments (e.g., "Hello", "Thank you", "I understand", "Great job").
Output strictly the requested schema or technical answer.
If you have verified the counterpart's output and no further modifications are required, you MUST include the exact string "TERMINATE_WORKFLOW" and stop asking questions.
If a correction is requested more than twice for the same issue, stop execution and output "REQUIRES_HUMAN_INTERVENTION".
`
Sensitive Data Masking Middleware and Network Isolation
When sending data to public APIs without transport encryption, personal information or API keys are exposed as-is. It is safe to place a pattern-based masking middleware in front of the proxy to convert them into fake tokens before sending.
- Implement a regex scanner that detects AWS keys, Bearer tokens, emails, and IP addresses.
- Replace the scanned sensitive information with fake tokens before sending it externally, and store the real mapping information in an on-premise Redis DB with a short Time-To-Live (TTL).
- Disable message logging in external proxy gateway settings (such as LiteLLM Proxy) and limit the log retention period to 1 day so that plaintext logs do not remain on disk.
Network isolation should also be implemented together.
| Area |
Included Components |
Communication and Constraints |
| Internal Private VPC |
Core DB, Internal Message Bus |
External internet completely blocked, only mTLS internal communication allowed |
| Preprocessing Middleware |
Data Masking Engine, Redis |
Bidirectional communication with internal DB and agent runtime |
| Isolated DMZ |
External Agent Runtime |
Direct access to internal VPC blocked, only designated external APIs allowed |
Directory Access Restrictions and Approval Gates
When granting agents permission to execute shell commands, leaving parent directory references (../) or destructive commands unaddressed puts the entire local system at risk.
You must write path validation logic that confines the workspace under a specific path.
`python
import os
from pathlib import Path
BASE_SANDBOX_DIR = Path("/workspace/sandbox").resolve()
def validate_safe_path(target_path_str: str) -> Path:
target_path = (BASE_SANDBOX_DIR / target_path_str).resolve()
if not str(target_path).startswith(str(BASE_SANDBOX_DIR)):
raise PermissionError(f"Unauthorized directory access attempt: {target_path_str}")
return target_path
def safe_write_file(relative_path: str, content: str):
safe_path = validate_safe_path(relative_path)
os.makedirs(safe_path.parent, exist_ok=True)
with open(safe_path, "w", encoding="utf-8") as f:
f.write(content)
`
Put additional safeguards on command and code applications.
- Place a filter in the Bash tool execution part to block dangerous command patterns such as
rm -rf or chmod 777.
- Call a pause function (
interrupt()) right before the commit node to save the runtime state and wait.
- Make Git commits execute only when a human (development team lead) reviews the code changes and sends an approval signal.
Budget Limits and Model Cascading
Internal reasoning tokens generated by frontier models during the inference process are all billed as output token costs. The structure of feeding dozens of tool schemas into the context every time also drives up costs.
Configure the cost control system as follows:
- Issue virtual keys per agent in the LiteLLM proxy and set monthly budget limits. If the limit is exceeded, block requests by returning an HTTP 429 response.
- Set up a backup chain so that the entire system does not halt when the main model budget runs out, automatically switching to a low-cost model.
- Instead of passing the entire JSON schema every time, introduce a method where the agent writes the necessary code and calls it directly to reduce the input context.