Claude Agent SDK
Connect Akto with applications built on the Claude Agent SDK
Akto Guardrails for the Claude Agent SDK provides real-time security validation for server-side AI agent applications. It plugs directly into the Claude Agent SDK's native callback system to validate prompts and tool calls before execution, validate agent responses after generation, and ingest all events into your Akto dashboard.
Key Features
Request Guardrails — Validates user prompts against Akto security policies before they reach the model
Response Guardrails — Validates agent responses against Akto security policies after generation
Tool Call Guardrails — Validates MCP and built-in tool calls before execution
Observability — Ingests all traffic (prompts, tool calls, responses) into the Akto dashboard
Sync & Async Modes — Block violations in real time or run in observe-only mode
Zero External Dependencies — Pure Python stdlib + asyncio; requires Python 3.8+
How It Works
The integration hooks into four points of the Claude Agent SDK lifecycle:
Hook Coverage
Hook
Event
Sync Mode (true)
Async Mode (false)
akto_user_prompt_submit
UserPromptSubmit
Validates prompt; blocks if denied
Passes through; no validation
akto_stop
Stop
Validates response; re-enters agent loop if denied
Ingests with response_guardrails=true (observational)
akto_pre_tool_use
PreToolUse
Validates tool call; blocks if denied
Passes through; no validation
akto_post_tool_use
PostToolUse
Ingests tool result (always)
Ingests tool result with guardrails flag (always)
Prerequisites
Python 3.8+
Claude Agent SDK installed
Akto Data Ingestion URL (
AKTO_DATA_INGESTION_URL)
Installation
No external packages required. Copy the three files into your project:
Download from GitHub:
Usage
Import and Create Hooks
create_hooks() returns four async callbacks bound to the client IP of the incoming request. Call it once per request or session.
If the client IP is not available, omit the argument — hooks fall back to 0.0.0.0.
Register Hooks with the Agent
Set Environment Variables
See Environment Variables for the full reference.
Run Your Agent
Sync vs Async Mode
AKTO_SYNC_MODE=true (default — blocking)
AKTO_SYNC_MODE=true (default — blocking)AKTO_SYNC_MODE=false (observe only)
AKTO_SYNC_MODE=false (observe only)Response Guardrails
The Stop hook runs response guardrails by sending the full conversation turn (user prompt + agent response) to Akto's /validate/response endpoint via the response_guardrails=true query parameter on the http-proxy API.
Sync mode behaviour when response is denied:
The hook returns {"continue_": True, "systemMessage": "<block reason>"} to re-enter the agent loop. The agent receives the system message and regenerates a safe response.
The Stop hook fires after the agent has finished generating. In streaming deployments the response may already be partially visible to the user. The hook causes a follow-up regeneration but does not retroactively suppress already-streamed content.
Environment Variables
AKTO_DATA_INGESTION_URL
(required)
Base URL for Akto's data ingestion service
AKTO_SYNC_MODE
true
true = block on violations; false = observe-only
AKTO_HOST
api.anthropic.com
Hostname written into request headers sent to Akto's HTTP proxy
AKTO_TIMEOUT
5
HTTP request timeout in seconds
AKTO_TOKEN
""
Authorization header value sent to AKTO_DATA_INGESTION_URL
MODE
argus
argus (default) or atlas
AKTO_CONNECTOR
claude_agent_sdk
Source label shown in the Akto dashboard
LOG_DIR
~/.claude/akto/logs
Directory for log files
LOG_LEVEL
INFO
Logging level (DEBUG, INFO, WARNING, ERROR)
LOG_PAYLOADS
false
Set to true to log full request/response bodies
Logging
All hooks write to a single log file:
Set LOG_PAYLOADS=true to log full request/response bodies (useful for debugging; disable in production).
Differences from Claude CLI Hooks
Invocation
Shell command via settings.json
Async Python callback function
Input format
JSON via stdin
input_data dict argument
Block (UserPromptSubmit)
print({"decision":"block",...})
return {"continue_": False, "systemMessage": ...}
Block (PreToolUse)
print({"decision":"block",...})
return {"hookSpecificOutput": {"permissionDecision": "deny", ...}}
Response guardrails
Stop hook (validate-response.py)
Stop hook via response_guardrails=true parameter
HTTP calls
Synchronous urllib
urllib wrapped in asyncio.to_thread
Configuration
Set by .sh wrapper scripts
Standard environment variables
AKTO_CONNECTOR default
claude_code_cli
claude_agent_sdk
Device/server ID
Derived from machine UUID
AGENT_ID env var
contextSource
ENDPOINT
AGENTIC (hardcoded)
Troubleshooting
Hooks Not Triggering
Guardrails Always Allowing (Fail-Open)
The hooks are fail-open by design — any network or API error allows the request through. Check logs for errors:
Common causes:
AKTO_DATA_INGESTION_URLnot set or unreachableAKTO_SYNC_MODEset tofalseGuardrail policies not configured in the Akto dashboard
Events Not Appearing in Dashboard
Response Guardrails Not Blocking
Confirm that AKTO_SYNC_MODE=true and that response guardrail policies are configured in the Akto dashboard under Settings → Guardrails. Response guardrails are a separate policy set from request guardrails.
Get Support
In-app
intercomsupport — message us from the Akto dashboardJoin our Discord community
Email [email protected]
Last updated