Policy entity
A policy is defined by the following properties:backend/syft_space/components/policies/entities.py:15
Policies are endpoint-scoped: each policy belongs to exactly one endpoint. Multiple policies can be attached to the same endpoint.
Policy types
Policy types implement theBasePolicyType protocol and provide:
Configuration schema
Each type defines required fields:Hook interface
All policy types implement pre and post hooks:backend/syft_space/components/policy_types/interfaces.py:94
Policy context
Policies receive execution context with request/response information:backend/syft_space/components/policy_types/interfaces.py:30
Key context fields
sender_email
sender_email
Verified email address extracted from SyftHub authentication token.Use cases:
- Rate limiting per user
- Usage tracking
- Access control lists
metadata
metadata
Additional context injected by the endpoint handler.Example: Accounting credentialsPolicies can read from and write to metadata to share state between hooks.
request
request
The full query request payload:
messages: User inputtemperature,max_tokens: Model parameterssimilarity_threshold,limit: Search parameterstransaction_token: Accounting token (if provided)
response
response
Only available in post hooks:
summary: Model-generated responsereferences: Dataset search results
Policy violation errors
Policies block requests by raising exceptions:backend/syft_space/components/policy_types/interfaces.py:8
Handler behavior:
backend/syft_space/components/endpoints/handlers.py:302
Execution flow
Policies execute in the endpoint query pipeline:Multiple policies per type
When multiple policies of the same type are attached to an endpoint, all configurations are passed to a single policy instance:- AND logic: All conditions must pass
- OR logic: Any condition can pass
- Custom: Policy-specific behavior
backend/syft_space/components/endpoints/handlers.py:285
Available policy types
Rate limit
Type name:rate_limit
Limits the number of requests per time window.
Configuration:
- Pre-hook: Checks if sender has exceeded rate limit
- Post-hook: No-op
- Prevent abuse
- Enforce API quotas
- Tier-based access (free vs paid users)
Accounting guard
Type name:accounting_guard
Enforces payment/credits before allowing access. Confirms transaction after response.
Configuration:
- Pre-hook: Validates user has sufficient credits, reserves cost
- Post-hook: Confirms transaction, deducts credits
- Paid API access
- Credit-based systems
- Pay-per-query models
Policy operations
Create policy
backend/syft_space/components/policies/handlers.py:84
Request schema:
Update policy
Partial updates (name, configuration merge):Configuration updates are shallow merged: new keys override existing, missing keys are preserved.
backend/syft_space/components/policies/handlers.py:164
Delete policy
backend/syft_space/components/policies/handlers.py:216
List policies by endpoint
backend/syft_space/components/policies/handlers.py:245
Grouped policy execution
Endpoint handler groups policies by type before execution:backend/syft_space/components/endpoints/handlers.py:285
This design allows:
- Efficient batching (one DB query for all policies)
- Type-specific aggregation logic
- Clean separation of concerns
Relationships
- Tenant: Each policy belongs to one tenant
- Endpoint: Each policy protects one endpoint (many policies per endpoint)
Example workflows
- Rate limiting
- Tiered access
- Accounting guard
Best practices
Layer multiple policies
Layer multiple policies
Combine rate limiting with accounting for comprehensive protection:
- Rate limit prevents abuse even for paid users
- Accounting ensures payment
Use descriptive names
Use descriptive names
Name policies by their purpose:
"Premium users: 1000/hour", "$0.01 per query"Test policies separately
Test policies separately
Create test endpoints with single policies to verify behavior before combining
Monitor policy violations
Monitor policy violations
Log
PolicyViolationError exceptions to understand usage patterns and adjust limitsHandle graceful degradation
Handle graceful degradation
If accounting service is down, policies can choose to:
- Block all requests (strict)
- Allow requests with warning log (graceful)
Next steps
Endpoints
Learn how policies integrate into the endpoint query flow
Overview
Review how all components work together