[BUG] Pipe Operation affects API Billing
Resolved 💬 4 comments Opened Jul 1, 2025 by annahambi Closed Aug 20, 2025
I am running a script from a subdirectory. If I attempt to pipe messages as shown below, Claude will raise a credit balance too low error. This happens only in the subdirectory... I tried to debug the issue with Claude Code, and it summarized it as attached below.
Code for SUCCESS and FAILURE
./my_script.sh contains the following parts:
claude \
"$PROMPT" \
--verbose \
--dangerously-skip-permissions \
--allowedTools "${TOOLS}" \
--model "${MODEL}" \
--output-format stream-json
Produces SUCCESS
Starting Claude analysis...
╭────────────────────────────────────────────────────────────────────────────────╮
│ ✻ Welcome to Claude Code! │
│ │
│ /help for help, /status for your current setup │
│ │
│ cwd: /home/anna/.../claude_code_script_run/319cfbc01768 │
╰────────────────────────────────────────────────────────────────────────────────╯
claude \
"$PROMPT" \
--verbose \
--dangerously-skip-permissions \
--allowedTools "${TOOLS}" \
--model "${MODEL}" \
--output-format stream-json \
2>&1 | tee ".result_all_messages.json" #<================ THIS LINE CAUSES ISSUES FOR SOME CRAZY REASON
Produces FAILURE "Credit balance is too low":
Starting Claude analysis...
{"type":"system","subtype":"init","cwd":"/home/anna/.../claude_code_script_run/319cfbc01768","session_id":"929c00eb-0e98-41cd-bdbd-0ef790ef1ba8","tools":["Task","Bash","Glob","Grep","LS","exit_plan_mode","Read","Edit","MultiEdit","Write","NotebookRead","NotebookEdit","WebFetch","TodoRead","TodoWrite","WebSearch"],"mcp_servers":[],"model":"claude-sonnet-4-20250514","permissionMode":"bypassPermissions","apiKeySource":"ANTHROPIC_API_KEY"}
{"type":"assistant","message":{"id":"ce94f27c-1815-4587-80ad-a332ec22867a","model":"<synthetic>","role":"assistant","stop_reason":"stop_sequence","stop_sequence":"","type":"message","usage":{"input_tokens":0,"output_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"server_tool_use":{"web_search_requests":0},"service_tier":null},"content":[{"type":"text","text":"Credit balance is too low"}]},"parent_tool_use_id":null,"session_id":"929c00eb-0e98-41cd-bdbd-0ef790ef1ba8"}
{"type":"result","subtype":"success","is_error":true,"duration_ms":1978,"duration_api_ms":0,"num_turns":1,"result":"Credit balance is too low","session_id":"929c00eb-0e98-41cd-bdbd-0ef790ef1ba8","total_cost_usd":0,"usage":{"input_tokens":0,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":0,"server_tool_use":{"web_search_requests":0},"service_tier":"standard"}}
Claude Code prompt processing completed with exit code 0.
Claude Code Summary:
Claude output:
... The ONLY difference is the 2>&1 | tee ".result_all_messages.json" pipe!
This suggests that when Claude Code's output is piped to tee, it somehow changes the billing/credit context based on the working directory. This could be:
1. Output buffering issue - piping might change how Claude Code handles session state
2. Process context - the pipe might affect how Claude Code identifies the session/billing context
3. File creation permissions - writing to .result_all_messages.json might trigger different security contexts
This is definitely a bug in Claude Code. The pipe operation shouldn't affect API billing at all.
For now, you could work around it by removing the logging to the JSON file, but you'd lose the detailed output capture.
Bug Report Details:
- Title: "Credit balance error when using output redirection from subdirectories"
- Description: Claude Code shows "Credit balance is too low" error when using 2>&1 | tee output redirection, but only when executed from subdirectories. Same command works fine from parent directory.
- Reproduction Steps:
a. Create subdirectory and run: claude "prompt" --allowedTools "tools" --model "model" 2>&1 | tee "file.json" → Fails with credit error
b. Run same command from parent directory → Works fine
c. Run without pipe from subdirectory: claude "prompt" --allowedTools "tools" --model "model" → Works fine
- Expected: Output redirection shouldn't affect billing/credits
- Actual: Directory + pipe combination triggers false credit errors
- Environment: Linux, same API key and environment in both cases
This is a critical bug since it makes Claude Code unreliable for scripted/automated usage depending on working directory and output handling.This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗