Skip to main content
The langchain-spicedb package provides LangChain tools that enable agents to check SpiceDB permissions before taking actions. These tools are particularly useful for building agentic RAG systems where the agent needs to verify access permissions before retrieving or operating on resources.

Installation

Setup

These tools require a running SpiceDB instance. See the SpiceDB provider page for setup instructions.

Environment setup

Tools

SpiceDBPermissionTool

Check if a single user has permission to access a specific resource.

Initialization

Parameters

  • spicedb_endpoint (str): SpiceDB server address (default: “localhost:50051”)
  • spicedb_token (str): Pre-shared key for SpiceDB authentication
  • resource_type (str): SpiceDB resource type (e.g., “document”, “article”)
  • subject_type (str): SpiceDB subject type (default: “user”)
  • fail_open (bool): If True, allow access on errors; if False, deny on errors (default: False)
  • use_tls (bool): Whether to use TLS for SpiceDB connection (default: False)

Usage with agents

Direct tool usage

SpiceDBBulkPermissionTool

Check permissions for multiple resources at once - useful when an agent needs to verify access to several documents before proceeding.

Initialization

Parameters

Same as SpiceDBPermissionTool (see above).

Usage with agents

Direct tool usage

How agents decide to call these tools

Agents use the tool name and description to decide when to invoke them:

Tool names

  • check_spicedb_permission - Single permission check
  • check_spicedb_bulk_permissions - Bulk permission check

Tool descriptions

Both tools have detailed descriptions that guide the agent:
  • When to use: “Use this tool before retrieving sensitive documents or taking actions that require authorization”
  • What it does: Checks if a user has permission to access a resource
  • What it returns: “true”/“false” or list of accessible resources

Influencing tool usage

To make agents more likely to check permissions:
  1. System Prompt: Include explicit security guidance
  2. Lower Temperature: Use temperature=0 for more deterministic behavior
  3. Clear system prompts: Provide explicit instructions for tool usage
  4. Few-Shot Examples: Include examples in the prompt showing the tool being used

Input Schema

SpiceDBPermissionTool

Important: The resource_id should be ONLY the ID portion, without the resource type prefix.✅ Correct: resource_id="doc1"❌ Incorrect: resource_id="article doc1" or resource_id="article:doc1"

SpiceDBBulkPermissionTool

Error handling

Fail closed (default)

By default, tools fail closed - if there’s an error checking permissions, access is denied:

Fail open

For development or specific use cases:

Complete example: Secure document agent

API reference

SpiceDBPermissionTool

  • name: "check_spicedb_permission"
  • description: Checks if a user has permission to access a resource
  • args_schema: SpiceDBPermissionInput
  • return_type: str (“true” or “false”)

SpiceDBBulkPermissionTool

  • name: "check_spicedb_bulk_permissions"
  • description: Checks if a user has permission to access multiple resources
  • args_schema: SpiceDBBulkPermissionInput
  • return_type: str (comma-separated list of accessible resources or denial message)