Debugging
Debug SwiftClaw agents effectively
Debugging
Learn how to effectively debug your SwiftClaw agents.
Debug Mode
Enable Debug Logging
# Enable debug mode
swiftclaw debug my-agent --enable
# Set log level
swiftclaw config my-agent --log-level debugView Debug Logs
# Tail debug logs
swiftclaw logs my-agent --level debug --follow
# Filter by component
swiftclaw logs my-agent --level debug --component model
# Search debug logs
swiftclaw logs my-agent --level debug --search "tool_call"Local Debugging
Run Agent Locally
# Run in development mode
swiftclaw dev my-agent
# Run with debug output
swiftclaw dev my-agent --debug
# Run with specific configuration
swiftclaw dev my-agent --config dev.jsonInteractive Debugging
from swiftclaw import Agent
agent = Agent(name="my-agent", debug=True)
# Enable breakpoints
import pdb
@agent.on_message
async def handle_message(message):
pdb.set_trace() # Debugger breakpoint
response = await agent.generate(message.content)
return responseRequest Tracing
Enable Tracing
# Enable request tracing
swiftclaw trace my-agent --enable
# View traces
swiftclaw trace my-agent --list
# View specific trace
swiftclaw trace my-agent --id trace-abc123Trace Details
# View trace timeline
swiftclaw trace my-agent --id trace-abc123 --timeline
# Output:
# 0ms Request received
# 5ms Authentication verified
# 10ms Memory search started
# 150ms Memory search completed
# 160ms Model inference started
# 2100ms Model inference completed
# 2105ms Tool call: search_database
# 2350ms Tool call completed
# 2355ms Response sent
# Total: 2355msTool Debugging
Debug Tool Calls
@agent.tool(debug=True)
async def search_database(query: str):
print(f"Tool called with query: {query}")
result = await db.search(query)
print(f"Tool returned {len(result)} results")
return resultTool Call Logs
# View tool call logs
swiftclaw logs my-agent --filter tool_calls
# View failed tool calls
swiftclaw logs my-agent --filter tool_calls.failedMemory Debugging
Inspect Memory
# View memory contents
swiftclaw memory inspect my-agent
# Search memory
swiftclaw memory search my-agent --query "user preferences"
# Export memory
swiftclaw memory export my-agent --output memory.jsonMemory Metrics
# View memory metrics
swiftclaw metrics my-agent --metric memory
# Memory usage by type
swiftclaw metrics my-agent --metric memory --breakdownModel Debugging
View Model Requests
# View model requests
swiftclaw logs my-agent --filter model.requests
# View model responses
swiftclaw logs my-agent --filter model.responses
# View model errors
swiftclaw logs my-agent --filter model.errorsToken Usage
# View token usage
swiftclaw metrics my-agent --metric tokens
# Token usage by request
swiftclaw analyze my-agent --tokens --period 24hPerformance Profiling
Profile Agent
# Run profiler
swiftclaw profile my-agent --duration 5m
# Output:
# Component Time Percentage
# Model Inference 2100ms 70%
# Memory Search 150ms 5%
# Tool Calls 250ms 8%
# Other 500ms 17%Identify Bottlenecks
# Analyze slow requests
swiftclaw analyze my-agent --slow-requests --threshold 2s
# Generate flame graph
swiftclaw profile my-agent --flamegraph --output profile.svgError Debugging
View Error Stack Traces
# View recent errors
swiftclaw errors my-agent --limit 10
# View error details
swiftclaw errors my-agent --error-id abc123
# View error stack trace
swiftclaw errors my-agent --error-id abc123 --stack-traceError Patterns
# Analyze error patterns
swiftclaw analyze my-agent --errors --period 7d
# Group errors by type
swiftclaw errors my-agent --group-by typeNetwork Debugging
View Network Requests
# View outgoing requests
swiftclaw logs my-agent --filter network.outgoing
# View failed requests
swiftclaw logs my-agent --filter network.failed
# View request latency
swiftclaw metrics my-agent --metric network-latencyTest Connectivity
# Test external API connectivity
swiftclaw test connectivity my-agent --api openai
# Test database connectivity
swiftclaw test connectivity my-agent --databaseDebugging Tools
Interactive Shell
# Open interactive shell
swiftclaw shell my-agent
# Execute commands
> agent.status()
> agent.memory.search("test")
> agent.generate("Hello")Remote Debugging
# Enable remote debugging
swiftclaw debug my-agent --remote --port 5678
# Connect with debugger
python -m debugpy --connect localhost:5678Common Debugging Scenarios
Debugging Slow Responses
# Enable performance tracing
swiftclaw trace my-agent --enable
# Make request
curl https://api.swiftclaw.io/agents/my-agent/invoke \
-H "Authorization: Bearer $TOKEN" \
-d '{"message": "test"}'
# View trace
swiftclaw trace my-agent --latest --timelineDebugging Memory Issues
# Add memory debugging
@agent.on_message
async def handle_message(message):
# Log memory before
print(f"Memory before: {agent.memory.size()}")
response = await agent.generate(message.content)
# Log memory after
print(f"Memory after: {agent.memory.size()}")
return responseDebugging Tool Failures
@agent.tool
async def problematic_tool(param: str):
try:
result = await external_api.call(param)
return result
except Exception as e:
# Log detailed error
agent.log.error(f"Tool failed: {e}", exc_info=True)
raiseDebug Configuration
Development Config
{
"debug": true,
"logging": {
"level": "debug",
"format": "detailed",
"includeStackTrace": true
},
"tracing": {
"enabled": true,
"sampleRate": 1.0
},
"profiling": {
"enabled": true
}
}Production Config
{
"debug": false,
"logging": {
"level": "info",
"format": "json",
"includeStackTrace": false
},
"tracing": {
"enabled": true,
"sampleRate": 0.1
},
"profiling": {
"enabled": false
}
}Performance Impact: Debug mode increases resource usage. Disable in production.
Best Practices
1. Use Structured Logging
agent.log.info("Processing request", extra={
"user_id": user_id,
"request_id": request_id,
"message_length": len(message)
})2. Add Context to Errors
try:
result = await risky_operation()
except Exception as e:
agent.log.error(
"Operation failed",
exc_info=True,
extra={"operation": "risky_operation", "params": params}
)
raise3. Use Correlation IDs
@agent.on_message
async def handle_message(message):
correlation_id = generate_correlation_id()
agent.log.info("Request started", extra={"correlation_id": correlation_id})
try:
response = await agent.generate(message.content)
agent.log.info("Request completed", extra={"correlation_id": correlation_id})
return response
except Exception as e:
agent.log.error("Request failed", extra={"correlation_id": correlation_id})
raiseNext Steps
How is this guide ?
Last updated on