Skip to content

feat(models): Claude CLI provider for Pro/Max subscription users#1434

Open
mhooooo wants to merge 1 commit intobytedance:mainfrom
mhooooo:feat/claude-cli-provider
Open

feat(models): Claude CLI provider for Pro/Max subscription users#1434
mhooooo wants to merge 1 commit intobytedance:mainfrom
mhooooo:feat/claude-cli-provider

Conversation

@mhooooo
Copy link
Copy Markdown

@mhooooo mhooooo commented Mar 26, 2026

Summary

Adds ClaudeCLIModel — a new LangChain BaseChatModel provider that wraps the Claude Code CLI in --print mode. This enables DeerFlow users with a Claude Pro or Max subscription to run DeerFlow without an API key at zero additional API cost.

Problem

Currently, using Claude with DeerFlow requires either:

  • An ANTHROPIC_API_KEY (pay-per-token), or
  • The existing ClaudeChatModel with OAuth tokens — which returns 400 invalid_request_error because OAuth tokens are scoped to Claude Code sessions and don't work with direct Messages API calls

Many users already have Claude Pro ($20/mo) or Max ($100-200/mo) subscriptions with generous usage included. There's no way to leverage these subscriptions with DeerFlow today.

Solution

ClaudeCLIModel spawns claude --print --output-format json as a subprocess, which authenticates using the CLI's built-in auth (claude /login). The CLI handles token management, refresh, and all API communication internally.

Config:

models:
  - name: claude-sonnet
    display_name: Claude Sonnet 4.6 (Max subscription)
    use: deerflow.models.claude_cli_provider:ClaudeCLIModel
    model: claude-sonnet-4-6
    max_tokens: 16384

Prerequisites:

curl -fsSL https://claude.ai/install.sh | bash
claude /login

Features

  • Implements BaseChatModel — works with LangGraph agents out of the box
  • bind_tools() support — tool schemas injected into system prompt for LangGraph compatibility
  • Parses JSON output including token usage, cost, duration, and session metadata
  • Filters interfering env vars (CLAUDECODE, NODE_OPTIONS, etc.) from subprocess
  • Configurable: model, max_turns, permission_mode, allowed/disallowed tools, timeout
  • Auto-discovers claude CLI from $PATH or well-known install locations

Tested with

  • Claude Sonnet 4.6 + Max subscription (5x rate limit tier)
  • DeerFlow lead agent with full middleware chain
  • macOS (Apple Silicon)

Test plan

  • Direct invocation: ClaudeCLIModel.invoke([HumanMessage("What is 2+2?")])"4"
  • LangGraph integration: Full lead agent conversation via /threads/{id}/runs/stream
  • Tool binding: bind_tools() with DeerFlow's built-in tools (bash, file ops, web search)
  • Error handling: timeout, CLI not found, non-zero exit code
  • Unit tests (happy to add if maintainers want them in this PR)

🤖 Generated with Claude Code

Adds `ClaudeCLIModel`, a LangChain `BaseChatModel` that wraps the
Claude Code CLI in `--print` mode. This enables DeerFlow users with
a Claude Pro or Max subscription to use DeerFlow without an API key
at zero additional cost.

Authentication is handled by the CLI's built-in auth (`claude /login`),
which stores credentials in the OS keychain. No `ANTHROPIC_API_KEY`
environment variable is needed.

The provider:
- Spawns `claude --print --output-format json` as a subprocess
- Implements `bind_tools()` for LangGraph agent compatibility
- Parses JSON output including token usage and cost metadata
- Filters environment variables that interfere with the subprocess
- Supports configurable model, max_turns, permission_mode, and timeouts

Config example:
  models:
    - name: claude-sonnet
      display_name: Claude Sonnet 4.6 (Max subscription)
      use: deerflow.models.claude_cli_provider:ClaudeCLIModel
      model: claude-sonnet-4-6
      max_tokens: 16384
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 26, 2026

CLA assistant check
All committers have signed the CLA.

@WillemJiang WillemJiang added the question Further information is requested label Mar 26, 2026
@WillemJiang
Copy link
Copy Markdown
Collaborator

@mhooooo, thanks for the contribution. You need to sign the CLA by clicking the button before we can review it.
Please submit the unit test at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

question Further information is requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants