AI Policy Generation
Use a configured LLM provider to generate Cedar policies from natural language descriptions.
Generate Policy
POST /api/ai-policy/generateAuthentication
Requires admin Bearer token.
Request Body
{
provider: string // Required: LLM provider to use (e.g. "openai", "anthropic")
model: string // Required: Model to use (e.g. "gpt-4o", "claude-sonnet-4-20250514")
messages: Array<{ // Required: Conversation messages describing the desired policy
role: string
content: string
}>
cedarSchema?: string // Optional: Cedar schema text (defaults to current schema)
}Example Request
curl -X POST http://localhost:3100/api/ai-policy/generate \
-H "Authorization: Bearer your-admin-token" \
-H "Content-Type: application/json" \
-d '{
"provider": "openai",
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Create a policy that allows the engineering tenant to use completion and embedding actions during business hours (9 AM to 6 PM)"
}
]
}'Success Response
{
"policy": "@id(\"engineering-business-hours\")\npermit (\n principal,\n action in [Action::\"completion\", Action::\"embedding\"],\n resource\n)\nwhen {\n principal.tenant == \"engineering\" &&\n context.hour >= 9 &&\n context.hour < 18\n};",
"usage": {
"input_tokens": 1250,
"output_tokens": 85
}
}Error Responses
| Status | Code | Description |
|---|---|---|
| 400 | MISSING_FIELDS | provider, model, and messages are required |
| 400 | PROVIDER_NOT_CONFIGURED | The specified provider is not configured |
| 500 | SCHEMA_ERROR | Failed to retrieve the Cedar schema |
| 500 | GENERATION_ERROR | LLM generation failed |
How It Works
- The system prompt includes the full Cedar grammar specification and your current Cedar schema
- Your message describes the desired policy in natural language
- The LLM generates valid Cedar policy syntax based on the schema
- The response includes the raw policy text ready to be added via the Policies API
Tips
- Be specific about entity types, actions, and conditions in your description
- Reference the actual entity names from your schema (e.g.,
Action::"completion", tenant names) - Review generated policies before activating them — always test with the audit logs
- The generated policy includes an
@id()annotation automatically