[BUG] Agent/Explore subagent crashes with "Cannot read properties of undefined (reading 'input_tokens')"

Status Closed — not planned
Reported on v2.1.76
Maintainer reply None cached
Activity 11 comments · opened Mar 16, 2026 · closed May 10, 2026

Bug Description

When using the Agent tool (with subagent_type: Explore or general-purpose), the call consistently fails with:

Error: Cannot read properties of undefined (reading 'input_tokens')

All basic tools (Glob, Grep, Read, Bash, Edit, Write) work fine. Only the Agent/subagent invocations are affected.

Steps to Reproduce

  1. Start a Claude Code CLI session (v2.1.76)
  2. Trigger any task that invokes the Agent tool, e.g.:
  • subagent_type: Explore — for codebase exploration
  • subagent_type: general-purpose — for multi-step research
  1. The agent call fails immediately with the error above
  2. Retrying produces the same error every time

Expected Behavior

The subagent should launch, perform its task, and return results.

Actual Behavior

The subagent call fails instantly with:

Error: Cannot read properties of undefined (reading 'input_tokens')

No partial results are returned. The error appears to originate from response parsing logic where the API response object's usage or usage.input_tokens field is undefined.

Environment

  • Claude Code version: 2.1.76 (latest on npm as of 2025-03-16)
  • OS: Windows 11 Home China (10.0.26200)
  • Node.js: v22.22.1
  • Shell: Git Bash (MINGW64)
  • Model: claude-opus-4-6

Workaround

Avoid using Agent/Explore subagents entirely. Use Glob, Grep, and Read tools directly for codebase searches. This works but loses the parallelism and autonomy benefits of subagents.

Additional Context

  • The error is 100% reproducible — every Agent call fails, not intermittent.
  • Network connectivity is fine (all other API calls succeed).
  • This may be related to how subagent API responses are parsed — possibly a missing null check on response.usage before accessing input_tokens.

View original on GitHub ↗

11 Comments

github-actions[bot] · 5 months ago

Found 1 possible duplicate issue:

  1. https://github.com/anthropics/claude-code/issues/30620

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

RobertoHePe · 5 months ago

Is there any solution for this already? It also happens when running claude code with minimax2.7 through openrouter

middiu · 5 months ago

was happening to me after switching to minimax2.7, switched back to 2.5 and it seems to be working fine.

yuluo-yx · 5 months ago

+1, I also encountered this problem; my model is GLM5.

seek-hope · 5 months ago

+1, and it seems to occur more often when execute complex tasks.

ritvij14 · 5 months ago

+1, exactly same for me, minimax 2.7 keeps breaking

sleepybyte23 · 5 months ago

same for me as well, minimax 2.7 keeps breaking

AntSwig · 5 months ago

I am also getting the same when using subagents within a session. I am on MacOS Tahoe, so it's not exclusive to Windows.

github-actions[bot] · 3 months ago

Closing for now — inactive for too long. Please open a new issue if this is still relevant.

ritvij14 · 3 months ago

Solution that worked for me

Use a proxy. this is the script I used:

#!/usr/bin/env node
const http = require("http");
const https = require("https");

const PORT = 3099;

function patchUsage(usage) {
  if (!usage || typeof usage !== "object") return usage;
  usage.input_tokens = usage.input_tokens || usage.prompt_tokens || 0;
  usage.output_tokens = usage.output_tokens || usage.completion_tokens || 0;
  usage.cache_read_input_tokens = usage.cache_read_input_tokens || 0;
  usage.cache_creation_input_tokens = usage.cache_creation_input_tokens || 0;
  usage.cache_creation = usage.cache_creation || 0;
  return usage;
}

function patchSSELine(line) {
  if (!line.startsWith("data: ")) return line;
  const payload = line.slice(6).trim();
  if (payload === "[DONE]") return line;
  try {
    const obj = JSON.parse(payload);
    if (obj.usage) obj.usage = patchUsage(obj.usage);
    if (obj.message && obj.message.usage)
      obj.message.usage = patchUsage(obj.message.usage);
    return "data: " + JSON.stringify(obj);
  } catch {
    return line;
  }
}

const server = http.createServer((req, res) => {
  const options = {
    hostname: "openrouter.ai",
    port: 443,
    path: "/api" + req.url,
    method: req.method,
    headers: {
      ...req.headers,
      host: "openrouter.ai",
      "accept-encoding": "identity",
    },
  };

  const upstream = https.request(options, (upRes) => {
    const isSSE = (upRes.headers["content-type"] || "").includes(
      "text/event-stream",
    );

    res.writeHead(upRes.statusCode, upRes.headers);

    if (!isSSE) {
      upRes.pipe(res);
      return;
    }

    // SSE: patch each line
    let buffer = "";
    upRes.on("data", (chunk) => {
      buffer += chunk.toString();
      const lines = buffer.split("\n");
      buffer = lines.pop(); // keep incomplete line
      for (const line of lines) {
        res.write(patchSSELine(line) + "\n");
      }
    });
    upRes.on("end", () => {
      if (buffer) res.write(patchSSELine(buffer) + "\n");
      res.end();
    });
  });

  upstream.on("error", (err) => {
    console.error("[proxy] error:", err.message);
    res.writeHead(502);
    res.end(JSON.stringify({ error: err.message }));
  });

  req.pipe(upstream);
});

server.listen(PORT, "127.0.0.1", () => {});

And then, in my .zshrc I am using a custom command:

openclaude() {
  # Start proxy if not already running
  if ! lsof -i :3099 -t &>/dev/null; then
    node ~/.claude/proxy.js &
    PROXY_PID=$!
    echo "[proxy] started (pid $PROXY_PID)"
    sleep 0.5  # give it a moment to bind
  else
    PROXY_PID=""
    echo "[proxy] already running, reusing"
  fi

  ANTHROPIC_BASE_URL="http://localhost:3099" \
  ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY" \
  ANTHROPIC_API_KEY="" \
  ANTHROPIC_MODEL="minimax/minimax-m2.7" \
  ANTHROPIC_DEFAULT_OPUS_MODEL="minimax/minimax-m2.7" \
  ANTHROPIC_DEFAULT_SONNET_MODEL="minimax/minimax-m2.7" \
  ANTHROPIC_DEFAULT_HAIKU_MODEL="minimax/minimax-m2.7" \
  CLAUDE_CODE_SUBAGENT_MODEL="minimax/minimax-m2.7" \
  ENABLE_TOOL_SEARCH=true \
  claude --tools "Agent,AskUserQuestion,Bash,Edit,Glob,Grep,LSP,Read,Skill,TaskCreate,TaskGet,TaskList,TaskUpdate,TaskStop,TaskOutput,TodoWrite,WebFetch,WebSearch,Write" "$@"

}

This way, it worked perfectly fine.

NOTE: i have shifted to using kimi 2.6 using Ollama, and this is what worked for me before moving to ollama. Overall, no issues in Ollama. Posting it here for folks who still wanna use Open Router, or something else that might be causing the same error message for them.

In case the above does not work for you, make sure to feed it to claude and ask it to fix it for you. Make sure to check your paths and stuff
github-actions[bot] · 2 months ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.