Skip to main content

Debugging

Effective debugging is essential for developing reliable AI applications. This guide covers tools and techniques for debugging AI SDK applications.

Logging and Inspection

Basic Logging

Log key information during development:

Inspecting Messages

Log the full conversation history:

Tracking Request IDs

Generate unique IDs for tracking:

Using AI SDK DevTools

The AI SDK provides built-in DevTools for debugging:

Setup

Starting DevTools

Open http://localhost:3001 to view:
  • Request/response history
  • Token usage statistics
  • Performance metrics
  • Error logs

Middleware for Debugging

Logging Middleware

Create custom logging middleware:
Usage:

Streaming Debugging

Tracking Stream Events

Debugging Stream Interruptions

Error Debugging

Detailed Error Information

Error Context Wrapper

Tool Call Debugging

Logging Tool Executions

Debugging Tool Input Validation

Network Debugging

Custom Fetch for Logging

Debugging Proxy Issues

RSC Debugging

Debugging Streamable UI

Debugging AI State

Browser DevTools

Network Tab

  1. Open browser DevTools (F12)
  2. Go to Network tab
  3. Filter by “Fetch/XHR”
  4. Look for API requests
  5. Inspect request/response bodies

Console Logging in Client Components

Best Practices

  1. Use structured logging: Log with context and request IDs
  2. Enable DevTools in development: Use @ai-sdk/devtools for insights
  3. Log at different levels: Debug, info, warn, error
  4. Track performance: Measure request duration and token usage
  5. Validate inputs: Catch errors early with schema validation
  6. Use error boundaries: Isolate failures in React components
  7. Test error paths: Ensure error handling works correctly
  8. Monitor production: Use logging/monitoring services

Next Steps