ClaudeCodeSDK.DebugMode (claude_code_sdk v0.0.1)
View SourceComprehensive debugging and diagnostics for Claude Code SDK.
This module provides tools for troubleshooting queries, analyzing performance, and diagnosing environment issues. Essential for development, testing, and production monitoring of Claude Code SDK usage.
Features
- Query Debugging: Detailed execution logging with timing
- Environment Diagnostics: CLI installation and auth status checks
- Performance Benchmarking: Multi-run performance analysis
- Message Analysis: Content statistics and error detection
- Connectivity Testing: Basic health checks and validation
Basic Usage
# Debug a specific query
messages = ClaudeCodeSDK.DebugMode.debug_query("Hello, Claude!")
# Run full environment diagnostics
ClaudeCodeSDK.DebugMode.run_diagnostics()
# Benchmark query performance
results = ClaudeCodeSDK.DebugMode.benchmark("Analyze this code", nil, 3)
# Analyze message statistics
stats = ClaudeCodeSDK.DebugMode.analyze_messages(messages)
Debug Output Example
🐛 DEBUG MODE ENABLED
Prompt: "Hello, Claude!"
Options: %ClaudeCodeSDK.Options{verbose: true, max_turns: 1}
✅ Auth: Authenticated as user@example.com
[0ms] system:init: session_id=abc123, model=claude-opus-4
[1250ms] assistant: "Hello! How can I help you today?" (35 chars)
[1680ms] result:success: cost=$0.003, turns=1
🏁 Debug completed in 1680ms with 3 messages
Environment Diagnostics
🔍 Running Claude Code SDK Diagnostics...
✅ CLI Status: Installed at /usr/local/bin/claude
Version: 1.2.3
✅ Authentication: Authenticated as user@example.com
📋 Environment:
Mix env: dev
Mock enabled: false
Elixir: 1.15.0
OTP: 26
🔌 Testing basic connectivity...
✅ Basic connectivity OK
✅ All systems operational!
Summary
Functions
Analyzes a stream of messages and provides comprehensive statistics.
Benchmarks a query and returns performance metrics.
Executes a query in debug mode with detailed logging and timing.
Formats a message for detailed inspection.
Executes a query with performance profiling.
Runs a diagnostic check of the SDK environment.
Functions
@spec analyze_messages(Enumerable.t()) :: map()
Analyzes a stream of messages and provides comprehensive statistics.
Examines message patterns, content metrics, performance data, and error conditions to provide insights into query execution and results.
Parameters
messages
- List or stream ofClaudeCodeSDK.Message
structs
Returns
- Map with detailed analysis results
Examples
messages = ClaudeCodeSDK.query("Analyze this code")
stats = ClaudeCodeSDK.DebugMode.analyze_messages(messages)
# %{
# total_messages: 5,
# message_types: %{assistant: 2, system: 1, result: 1, user: 1},
# total_cost_usd: 0.025,
# duration_ms: 3420,
# content_length: 1523,
# tools_used: ["Read", "Grep"],
# session_id: "abc123",
# success: true,
# errors: []
# }
Analysis Fields
total_messages
- Count of all messagesmessage_types
- Breakdown by message typetotal_cost_usd
- Total API cost (if available)duration_ms
- Total execution timecontent_length
- Total character count of text contenttools_used
- List of tools invoked during executionsession_id
- Session identifiersuccess
- Whether query completed successfullyerrors
- List of any error conditions encountered
@spec benchmark(String.t(), ClaudeCodeSDK.Options.t() | nil, pos_integer()) :: map()
Benchmarks a query and returns performance metrics.
Parameters
prompt
- The query promptoptions
- Optional ClaudeCodeSDK.Optionsruns
- Number of times to run the query (default: 1)
Returns
- Map with benchmark results
Examples
iex> results = ClaudeCodeSDK.DebugMode.benchmark("Hello", nil, 3)
%{
runs: 3,
avg_duration_ms: 1523,
min_duration_ms: 1420,
max_duration_ms: 1650,
avg_cost_usd: 0.015
}
@spec debug_query(String.t(), ClaudeCodeSDK.Options.t() | nil) :: [ ClaudeCodeSDK.Message.t() ]
Executes a query in debug mode with detailed logging and timing.
Provides comprehensive debug output including authentication status, timing information for each message, and final statistics. Automatically enables verbose mode and catches/reports any errors.
Parameters
prompt
- The query prompt to debugoptions
- OptionalClaudeCodeSDK.Options
(verbose will be auto-enabled)
Returns
- List of messages with complete debug trace
Examples
# Basic debug query
messages = ClaudeCodeSDK.DebugMode.debug_query("Hello")
# Debug with custom options
options = %ClaudeCodeSDK.Options{max_turns: 3}
messages = ClaudeCodeSDK.DebugMode.debug_query("Complex task", options)
Output Format
🐛 DEBUG MODE ENABLED
Prompt: "Hello, Claude!"
Options: %ClaudeCodeSDK.Options{verbose: true, max_turns: 1}
✅ Auth: Authenticated as user@example.com
[0ms] system:init: session_id=abc123, model=claude-opus-4
[1250ms] assistant: "Hello! How can I help you today?" (35 chars)
[1680ms] result:success: cost=$0.003, turns=1
🏁 Debug completed in 1680ms with 3 messages
@spec inspect_message(ClaudeCodeSDK.Message.t()) :: String.t()
Formats a message for detailed inspection.
Parameters
message
- A ClaudeCodeSDK.Message struct
Returns
- Formatted string representation
Examples
iex> ClaudeCodeSDK.DebugMode.inspect_message(message)
"Message[assistant]: "Hello, world!" (15 chars)"
@spec profile_query(String.t(), ClaudeCodeSDK.Options.t() | nil) :: {[ClaudeCodeSDK.Message.t()], map()}
Executes a query with performance profiling.
Similar to debug_query/2
but focuses on performance metrics,
memory usage, and execution timing rather than detailed content logging.
Parameters
prompt
- The query promptoptions
- OptionalClaudeCodeSDK.Options
Returns
{messages, profile}
where profile contains performance data
Examples
{messages, profile} = ClaudeCodeSDK.DebugMode.profile_query("Complex task")
IO.puts("Peak memory: #{profile.peak_memory_mb}MB")
IO.puts("Execution time: #{profile.execution_time_ms}ms")
@spec run_diagnostics() :: :ok
Runs a diagnostic check of the SDK environment.
Checks:
- CLI installation
- Authentication status
- Basic connectivity
- Environment configuration
Examples
iex> ClaudeCodeSDK.DebugMode.run_diagnostics()
🔍 Running Claude Code SDK Diagnostics...
✅ CLI Status: Installed at /usr/local/bin/claude
...