Agentic AI Security: Sandboxing LLM Tool Calls in Production
When you give a language model the ability to call tools — run code, query databases, browse the web — you've created an autonomous execution surface. Most tutorials skip the part where that surface gets exploited. This post covers practical steps for sandboxing LLM tool calls before they reach production. No theory: concrete code patterns that limit the blast radius when something goes wrong.…
When granting a language model the capability to invoke tools — executing code, querying databases, browsing the web — an autonomous execution surface emerges. Numerous tutorials inadequately address this potential for exploitation. This account outlines practical measures for sandboxing LLM tool calls ahead of production deployment, focusing on pragmatic code patterns to contain unintended consequences.
The distinction between tool calls and standard LLM integrations is critical. While conventional LLM interactions are relatively contained — input receives text output — agent architectures fundamentally alter the risk landscape. The tool execution phase directly influences system behavior. A maliciously manipulated prompt injected into a document the agent processes could trigger data exfiltration.
An unchecked shell tool enables the model to execute any command. An HTTP tool lacking domain limitations may initiate SSRF attacks against your internal network. These are not theoretical scenarios; they mirror the same attack vectors that have historically plagued web applications, now applied to any agentic system you construct.
The most effective foundational safeguard is a rigid allowlist approach, treating the tool registry as the primary perimeter defense. By strictly defining what tools exist and what parameter sets they accept, you prevent the model from autonomously generating tool calls. Utilizing JSON Schema validation for parameter conformity adds an additional layer of security.
The Python implementation demonstrates a ToolRegistry class that enforces this approach: importing necessary modules, defining ToolSpec dataclasses for tool specifications, and implementing a ToolRegistry class to manage registered tools. Importantly, the registry raises a ValueError if an unknown tool is invoked, ensuring model prompt output remains within predefined boundaries.
Path traversal protection introduces a second defense layer, where the sandboxed handler performs additional checks on file paths, blocking potential unauthorized access. Moving beyond raw subprocess executions, the account cautions against using shell=True with model-generated inputs, highlighting the risk of command injection.
Instead, it presents a Python code sandboxing strategy that creates a named temporary file, writes the user-provided Python code, and executes it within a restricted subprocess environment. This approach limits the potential damage by confining execution to a controlled space with bounded resources, preventing any possibility of the model escaping the isolated environment.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.