Nadhebe
tutorials

Claude Code Troubleshooting Guide: Fixing OAuth Errors, Exit Code 2, and Rate Limits

A comprehensive troubleshooting guide resolving Claude Code CLI errors, including OAuth token refresh loops, exit code 2 script failures, and API rate limit freezes.

Nadhebe Editorial Team Nadhebe Editorial Team
· · 2 min read
GPU Lab Verified
Vintage editorial illustration of terminal error logs, red alert badges, and fix checklists on a soft terracotta background
On this page

Claude Code Troubleshooting Guide: Fixing OAuth Errors, Exit Code 2, and Rate Limits

Operating Claude Code inside complex development environments occasionally triggers runtime failures, API rate limits, or unexpected process freezes.

This guide provides tested, step-by-step solutions for resolving the 5 most common Claude Code CLI errors.


Troubleshooting Quick Reference Matrix

 ┌────────────────────────────────────────────────────────────────────────┐
 │                   CLAUDE CODE ERROR RESOLUTION FLOW                    │
 └───────────────────────────────────┬────────────────────────────────────┘

           ┌─────────────────────────┼─────────────────────────┐
           ▼                         ▼                         ▼
┌──────────────────────┐  ┌──────────────────────┐  ┌──────────────────────┐
│  OAUTH / AUTH ERROR  │  │  EXIT CODE 2 HOOK    │  │  RATE LIMIT (429)    │
├──────────────────────┤  ├──────────────────────┤  ├──────────────────────┤
│ • Run `claude logout`│  │ • PreToolUse blocked │  │ • Compact session    │
│ • Clear session JSON │  │ • Inspect hook log   │  │ • Switch to API key  │
└──────────────────────┘  └──────────────────────┘  └──────────────────────┘
Error MessageRoot CauseImmediate Fix
Authentication failed: Invalid tokenExpired OAuth session tokenRun claude logout && claude login
Tool execution cancelled (Exit Code 2)PreToolUse guardrail blocked commandCheck .claude/settings.json hook rules
API Error 429: Rate Limit ExceededReached hourly Pro/Team request capRun /compact or set ANTHROPIC_API_KEY
ENOSPC: System limit reachedFile watcher limit exceededIncrease fs.inotify.max_user_watches
Process hangs on git diffGiant lockfile diff (e.g., package-lock.json)Add lockfiles to .claudeignore

1. Resolving OAuth Token Expiration Loops

If the CLI continually prompts you to log in via browser:

# 1. Terminate active Claude processes
$ killall claude

# 2. Clear stored credentials file
$ rm -rf ~/.claude/session.json

# 3. Re-authenticate
$ claude login

2. Preventing Workspace Freezes with .claudeignore

Prevent Claude Code from attempting to parse giant minified assets or lockfiles by creating a root .claudeignore file:

node_modules/
dist/
.git/
package-lock.json
pnpm-lock.yaml
*.log

Frequently asked questions

Why does Claude Code return 'Exit Code 2' during tool execution?

Exit code 2 indicates that a synchronous PreToolUse hook script intercepted and blocked a dangerous or unapproved terminal command.

How do I fix the 'Invalid OAuth Token' error in Claude Code CLI?

Run `claude logout` followed by `claude login` to clear cached tokens stored in `~/.claude/session.json`.

Sources & references

  1. [1]Claude Code CLI Documentation
Nadhebe Editorial Team

Nadhebe Editorial Team

The collective editorial desk, technical writers, and hardware validation engineers at Nadhebe. All publications undergo multi-stage peer review and physical GPU lab validation.

Includes Free AI Starter Kit

The Weekly AI Engineering Briefing

Join AI engineers building with Claude, MCP, Gemini, and open-source models. Received by developers, researchers, and technical founders.