Description
Summary
When agent executions fail due to Claude Code errors (e.g. billing errors, auth failures), the backend logs Unknown error and returns a 500 to the caller. The actual error message from Claude Code's stream-json output (e.g. Credit balance is too low, Invalid API key) is not captured or surfaced. This makes debugging execution failures difficult without manual SSH into the agent container.
Component
Agent Runtime / Headless Task Execution
Priority
P2
Error
What the backend logs:
ERROR:agent_server.services.claude_code:[Headless Task] Task {id} failed (exit 1): Unknown error
INFO: 172.28.0.5:xxxxx - "POST /api/task HTTP/1.1" 500 Internal Server Error
What Claude Code actually returns (visible only when running manually inside the container):
{"type":"result","subtype":"success","is_error":true,"result":"Credit balance is too low","error":"billing_error"}Or in auth failure case:
{"type":"result","subtype":"success","is_error":true,"result":"Invalid API key · Fix external API key","error":"authentication_failed"}Root Cause
The agent server's claude_code service parses Claude Code's stream-json output but does not extract the error message from failed executions. When Claude Code exits with code 1, the final result event contains the actual error in the result and error fields, but the agent server discards this and reports "Unknown error".
Reproduction Steps
- Set an invalid or zero-balance
ANTHROPIC_API_KEYon any agent - Send a task to the agent (via UI, API, or schedule)
- Execution fails in ~2 seconds
- Check backend/agent logs — only "Unknown error" is shown
- Manually run
claude --print --verbose --output-format stream-json "hello"inside the agent container to see the real error
Expected Behavior
- The execution log should capture and display the actual error from Claude Code (
billing_error,authentication_failed, etc.) - The execution record in the database should store the error message
- The UI/API should show the real error instead of "Unknown error"
- The backend scheduler log should include the error detail:
Task execution failed: Credit balance is too lowinstead ofTask execution failed: Unknown error
Suggested Fix
Parse the final result event from Claude Code's stream-json output. When is_error is true, extract the result (human-readable message) and error (error type) fields and use them in the failure log and execution record.
Environment
- Trinity version:
a453e32 - Claude Code version: 2.1.62
Related
- Agent server claude_code service (headless task execution)
- Scheduler service (reports same "Unknown error" for scheduled tasks)
Activity
vybe commentedon Mar 21, 2026
Closing — fixed by commit 7d37316. _diagnose_exit_failure() now provides meaningful error messages with exit codes, rate limit detection, and actionable hints instead of generic 'Unknown error'.
Merge origin/dev into feature/1063-vue-router5-vite8