D2 SDK Documentation

Enterprise-grade RBAC for AI agents. Secure your LLM tools with a single decorator.

What is D2?

D2 (Detect and Deny) is a Python SDK that provides Role-Based Access Control (RBAC) for tools invoked by Large Language Models (LLMs). It ensures that an LLM can only execute a tool if the end-user's role grants explicit permission.

Key Features:

  • • Single decorator protection
  • • Default-deny security
  • • Sub-5 second revocation
  • • Local and cloud modes
  • • Zero infrastructure

Use Cases:

  • • AI agent tool authorization
  • • LLM function calling security
  • • Multi-tenant AI applications
  • • Enterprise AI governance
Core Concepts

@d2_guard Decorator

A simple decorator that wraps any function intended to be a tool. It handles the entire authorization flow.

Signed Policy Bundle

A central policy server provides a security policy as a signed JSON Web Signature (JWS). The signature guarantees the policy's authenticity and integrity.

Local Policy Cache

To achieve high performance, the signed policy is fetched and cached locally within the application. Tool-use decisions are made against this cache, avoiding network latency on the hot path.

User Context Propagation

The user's identity and roles are made available to the decorator implicitly using Python's contextvars, keeping tool function signatures clean.