skillfed

toolguard

Policy adherence code generation for guarding AI agent tools

toolguard v0.2.21 171.8K downloads/30d#10,356 on PyPI
Permissive license Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for… (full text in the JSON record) Active released

What it is and what it does

Toolguard is a two-phase system for enforcing business policies on AI agent tool calls. It bridges the gap between traditional hard-coded policy enforcement and the non-deterministic best-effort approach of appending policies to agent prompts. The package generates deterministic Python guard code that validates tool invocations against policy constraints before execution, preventing unauthorized calls or unsafe parameter values.

The workflow is split into buildtime (policy analysis and code generation) and runtime (guard execution). At buildtime, you provide a policy document and a set of tools (as Python functions or OpenAPI specs), and the package uses an LLM to extract policy requirements, generate test cases, and produce executable guard code. At runtime, you load the generated guards and invoke tools through a guard wrapper that checks preconditions before allowing execution. The package depends on langchain-core, litellm, pydantic, and testing utilities to orchestrate LLM calls, manage tool schemas, and validate generated code.

Use it for:

  • Enforce financial transaction limits or approval workflows in agent-driven banking systems before tools execute.
  • Prevent unauthorized data access by validating agent tool calls against role-based policies before database queries run.
  • Block division-by-zero or invalid parameter combinations in computational tools by checking preconditions deterministically.
  • Ensure compliance with data residency or privacy policies by guarding tool invocations that access sensitive data.
  • Generate audit trails of policy checks by logging guard evaluations before tool execution.
  • Validate multi-step agent workflows against operational constraints (e.g., 'do not multiply if operand KDI value equals X').

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Generates deterministic Python code that enforces business policy constraints on AI agent tool invocations before they execute, preventing policy violations at the tool level.

Yes, if you need deterministic policy enforcement on AI agent tools and can invest in the buildtime setup (policy document, LLM configuration, tool specs). The package is actively maintained, has low install friction, and solves a real problem in agentic systems. Not suitable if you need runtime-only policy injection without code generation, or if you lack access to an LLM provider for the buildtime phase.

Install

toolguard on PyPI

pip

pip install toolguard

uv

uv add toolguard

poetry

poetry add toolguard

Installing toolguard

Before you install

Low install friction with a pure Python wheel. Active maintenance (45 days since latest release). Depends on 12 runtime packages including langchain-core, litellm, pydantic, and pytest utilities—a substantial but standard AI/LLM stack.

License in practice

Apache License 2.0 (permissive). You may use, modify, and distribute this package freely in commercial and private projects, provided you include the license and document any changes.

Quickstart

pip install toolguard

from toolguard.buildtime import generate_guard_specs, LitellmModel
from toolguard.runtime import load_toolguards, ToolFunctionsInvoker

# Step 1: Generate specs from policy document and tools
specs = await generate_guard_specs(policy_text, tools, llm=llm_model)

# Step 2: Generate guard code
guards = await generate_guards_code(tool_specs=specs, tools=tools, llm=llm_model)

# Step 3: Load and use at runtime
with load_toolguards("output") as toolguard:
    invoker = ToolFunctionsInvoker(tools)
    await toolguard.guard_toolcall("tool_name", {"param": value}, invoker)

Requires Python 3.10+. Buildtime API requires an LLM provider (e.g., Azure OpenAI) configured via litellm. Policy documents and tool OpenAPI specs must be provided.

Verify before relying

  • Whether the generated guard code is guaranteed to be deterministic or only intended to be so in practice.
  • Performance overhead of guard evaluation at runtime for large numbers of tools or complex policies.
  • How well the tool handles edge cases in policy interpretation or ambiguous policy language.
  • Support for dynamic policy updates without regenerating all guard code.

Package facts

License Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for… (full text in the JSON record) (permissive)
Python support supports the current Python release (<3.15,>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies 12 — datamodel-code-generator, fastmcp, langchain-core, litellm, loguru, markdown, pydantic, pyright, pytest-asyncio, pytest-json-report, pytest, smolagents
Maintenance actively maintained — 45 days since the last release
First released
Downloads 171,769/month — #10,356 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: toolguard-0.2.21-py3-none-any.whl

License :: OSI Approved :: Apache Software LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3

Tags

ai agent policy enforcementtool guard constraintspolicy compliance code generationagent tool preconditionsbusiness policy guardrailsdeterministic policy validationagent tool policy checks
agent-guardrailspolicy-as-codellm-codegen

More Artificial Intelligence packages

Further reading