Skip to content

feat: explain on the local Claude Code (M20) - #48

Merged
jgrodziski merged 3 commits into
mainfrom
feat/m20-claude-code-provider
Nov 10, 2026
Merged

jgrodziski merged 3 commits into
mainfrom
feat/m20-claude-code-provider

Conversation

@jgrodziski

Copy link
Copy Markdown
Contributor

Summary

M20 (PLAN.md §21, Phase 18): codegraph explain can run on the local Claude Code install — no API key — as a third adapter behind the existing LlmClient port.

  • feat(llm) — M20a. claudeCodeClient spawns the user's own claude -p --output-format json --json-schema --system-prompt --tools "" --safe-mode --no-session-persistence, the user prompt on stdin (a context pack can exceed the 128 KB per-argument limit), and reads structured_output / usage / total_cost_usd / modelUsage into the port's response; every failure maps onto the port's one error type. --safe-mode, never --bare (which drops the OAuth login). findClaudeCode is pure: --claude-path → CLAUDE_CODE_PATH → PATH → known install locations. auto precedence stays OpenRouter > Cloudflare > Claude Code: an exported key is deliberate, an installed CLI ambient. No SDK added; the Anthropic API adapter is a named empty slot (§21.6), not a stub.
  • feat(cli) — M20b. --provider claude-code, --claude-path FILE; --model defaults per provider (sonnet) and is resolved before the plan, so --dry-run/--estimate show it; --max-tokens is noted and ignored. Docs, README, CLAUDE.md and the website's explain pages name the port, the three adapters and the empty slot.
  • fix(explain) — the estimate. The live run billed 307 896 prompt tokens for an estimate of 114 136: every claude -p prompt carries Claude Code's scaffolding and the response schema. promptOverhead(provider) (in @codegraph/llm) is handed to planRun as a plain number — the walk still never learns a provider. Re-estimated: 280 481; operations within 1 % of what was billed. Key providers unchanged, byte for byte.

Live run (Java fixture, Claude Code 2.1.322)

calls 68, 0 failed
records 79 — 70 explained (stamped claude-sonnet-5-5), 9 templated
side-car decodeInsights clean, not truncated, header provider: "claude-code", models sonnet/sonnet
usage (eof) 307 896 prompt + 54 688 completion tokens, $1.03 (total_cost_usd, list price)

Not fixed here

Types (×1.28) and modules (×1.38) are still estimated low, on every provider (PetClinic on OpenRouter: types ×1.20…1.38): the dependency explanations a prompt inlines do not exist at plan time. Recorded in PLAN §21.4.

Test plan

  • @codegraph/llm 96, @codegraph/insights 163, @codegraph/cli 1 048 tests green; red-first for the estimate fix (three new tests)
  • pnpm -r typecheck clean; eslint clean on the touched files
  • All other suites green; analyzer/test/scale.test.ts timed out once under full-workspace load (known flake) and passes alone
  • Live run above; --estimate re-run on the fixture for both providers
  • website build not checked locally (snap Hugo 0.169, issue pnpm -r build fails on a fresh clone: unknown output format "llms" for kind "home" #23) — CI's Hugo job covers it

🤖 Generated with Claude Code

https://claude.ai/code/session_01PKCVh1q6FUhWzQi6KYtoTz

jgrodziski and others added 3 commits November 10, 2026 10:36
M20a. A third adapter behind the LlmClient port: claudeCodeClient
runs the user's own `claude -p --output-format json --json-schema
--system-prompt --tools "" --safe-mode --no-session-persistence`,
the user prompt on stdin, and reads structured_output, usage,
total_cost_usd and modelUsage into the port's response; every
failure maps onto the port's one error type. --safe-mode, never
--bare, which drops the OAuth login.

findClaudeCode is pure: explicit path, then CLAUDE_CODE_PATH, then
PATH, then the known install locations; an explicit path that does
not exist never falls through. resolveProvider takes a probe and
keeps auto's precedence OpenRouter > Cloudflare > Claude Code: an
exported key is deliberate, an installed CLI ambient.

Tested against a fake process and a recorded result; no SDK added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PKCVh1q6FUhWzQi6KYtoTz
M20b. `codegraph explain --provider claude-code [--claude-path FILE]`;
--model defaults per provider and is resolved before the plan, so
--dry-run and --estimate show `sonnet`; --max-tokens is ignored on
Claude Code with a note. PLAN §21 (Phase 18) records the port, the
three adapters and the Anthropic API as a named empty slot; docs,
README, CLAUDE.md and the website's explain pages follow.

Live run on the Java fixture (Claude Code 2.1.322): 68 calls, 0
failed, 79 records stamped claude-sonnet-5-5, decode-clean, $1.03.
Prompt tokens come out ~2.7x the estimate: Claude Code adds ~2 500
tokens of scaffolding per call that the estimate does not count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PKCVh1q6FUhWzQi6KYtoTz
The M20b live run billed 307 896 prompt tokens for an estimate of
114 136: every `claude -p` prompt carries Claude Code's scaffolding
and the response schema, which it sends as prompt. 48 operation
calls came in +2 292…+2 617 over the bare estimate.

promptOverhead(provider) in @codegraph/llm says what a transport
adds: nothing on the key providers (PetClinic on OpenRouter: +115
median per operation call), CLAUDE_CODE_SCAFFOLD_TOKENS (1 850) plus
the schema on Claude Code. planRun takes it as a plain number, so
the walk still never learns a provider; the CLI hands it over and
both estimate renderings say what was added.

Java fixture, re-estimated: 280 481 (was 114 136); operations within
1 % of what was billed. Types (x1.28) and modules (x1.38) stay under,
as on OpenRouter: the dependency explanations a prompt inlines do
not exist at plan time — a gap every provider shares, not fixed here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PKCVh1q6FUhWzQi6KYtoTz
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
codegraph 7b514bc Commit Preview URL

Branch Preview URL
Nov 10 2026, 10:56 AM

@jgrodziski
jgrodziski merged commit 6790c6c into main Nov 10, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant