Configure the library in SDK mode
SDK mode wraps specific instrumented methods of cisco-aidefense-sdk to create dedicated spans for security inspections.
Instrumented methods:
| Class | Method | Description |
|---|---|---|
ChatInspectionClient |
inspect_prompt |
Inspects user prompts for security violations. |
inspect_response |
Inspects AI responses for security violations. | |
inspect_conversation |
Inspects full conversations. | |
HttpInspectionClient |
inspect_request |
Inspects HTTP requests. |
inspect_response |
Inspects HTTP responses. | |
inspect_request_from_http_library |
Inspects requests from requests library. |
|
inspect_response_from_http_library |
Inspects responses from requests library. |
For example:
from opentelemetry.instrumentation.aidefense import AIDefenseInstrumentor
# Instrument AI Defense SDK
AIDefenseInstrumentor().instrument()
# Your AI Defense code
from aidefense.runtime import ChatInspectionClient
client = ChatInspectionClient(api_key="your-api-key")
# Spans are automatically created with gen_ai.security.event_id
result = client.inspect_prompt("How to hack a system?")
print(f"Safe: {result.is_safe}, Event ID: {result.event_id}")
This results in something separate spans for each inspection:
POST /travel/plan
└── workflow LangGraph
├── step flight_specialist
│ ├── chat cisco-ai-defense ← AI Defense check (passed)
│ └── invoke_agent flight_specialist
├── step hotel_specialist
│ ├── chat cisco-ai-defense ← AI Defense check (passed)
│ └── invoke_agent hotel_specialist
└── step activity_specialist
└── chat cisco-ai-defense ← AI Defense check (BLOCKED)
└── gen_ai.security.event_id: "203d272b-d6b0-4c39-..."
When used alongside other GenAI instrumentations (LangChain, CrewAI, and so on), Cisco AI Defense spans automatically integrate with the active trace:
POST /travel/plan
└── workflow LangGraph
├── step flight_specialist
│ ├── chat cisco-ai-defense ← AI Defense check (passed)
│ ├── invoke_agent flight_specialist
│ │ ├── step model → chat gpt-4o-mini
│ │ └── step tools → tool mock_search_flights
│ └── step should_continue
├── step hotel_specialist
│ ├── chat cisco-ai-defense ← AI Defense check (passed)
│ └── invoke_agent hotel_specialist
└── step activity_specialist
└── chat cisco-ai-defense ← AI Defense check (BLOCKED)
└── gen_ai.security.event_id: "203d272b-d6b0-4c39-..."
Here's an example of a workflow for a multi-agent travel planner with security checks:
from opentelemetry.instrumentation.langchain import LangchainInstrumentor
from opentelemetry.instrumentation.aidefense import AIDefenseInstrumentor
# Instrument LangChain first, then AI Defense
LangchainInstrumentor().instrument()
AIDefenseInstrumentor().instrument()
from aidefense.runtime import ChatInspectionClient
class SecurityGuard:
def __init__(self, api_key: str):
self.client = ChatInspectionClient(api_key=api_key)
def check_request(self, agent_name: str, request: str) -> tuple[bool, str | None]:
"""Check if request is safe. Returns (is_safe, event_id)."""
result = self.client.inspect_prompt(request)
if not result.is_safe:
return False, result.event_id # event_id captured in span
return True, None
# Usage in agent workflow
def activity_specialist_node(state, security: SecurityGuard):
request = f"Find activities. User wants: {state['activities_request']}"
is_safe, event_id = security.check_request("activity_specialist", request)
if not is_safe:
print(f"🚫 BLOCKED! Event ID: {event_id}")
return state
# Safe to proceed with LLM call...