Skip to content

Add Claude Code LM integration for DSPy #8574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

richardwhiteii
Copy link

Summary

  • Adds ClaudeCodeLM client for direct LLM access via Claude Code CLI
  • Provides OpenAI-compatible response format for seamless DSPy integration
  • Includes comprehensive example with multiple usage patterns

Features

  • Direct LLM inference through Claude CLI subprocess calls
  • Support for different Claude models (sonnet, opus, etc.)
  • Multi-turn agentic conversations via --max-turns parameter
  • JSON output parsing with robust error handling
  • No API keys required - uses Claude Code CLI authentication

Usage

import dspy
from dspy.clients.claude_code_lm import create_claude_code_lm

# Create Claude Code LM instance
lm = create_claude_code_lm(claude_model="sonnet", max_turns=3)

# Use with DSPy
dspy.configure(lm=lm)
predictor = dspy.Predict("question -> answer")
result = predictor(question="What is machine learning?")

Prerequisites

  • Claude Code CLI: npm install -g @anthropic-ai/claude-code
  • Authentication: claude auth login

Test plan

  • Verify Claude Code CLI integration works
  • Test OpenAI-compatible response format
  • Validate multi-turn conversations
  • Check error handling for missing CLI
  • Run comprehensive examples

Author: Richard White

This integration provides direct LLM access through Claude Code CLI, enabling
DSPy to use Claude models via the official Anthropic CLI tool.

Key features:
- Direct LLM inference through Claude CLI subprocess calls
- OpenAI-compatible response format for seamless DSPy integration
- Support for different Claude models (sonnet, opus, etc.)
- Multi-turn agentic conversations via --max-turns
- JSON output parsing with error handling
- Comprehensive example with various usage patterns

The ClaudeCodeLM client converts DSPy messages to Claude CLI prompts and
parses JSON responses back to OpenAI format, providing immediate access
to Claude models without requiring API keys or additional setup beyond
the Claude Code CLI installation.

Author: Richard White
@beapirate
Copy link

beapirate commented Jul 29, 2025

(not a dspy mainainer) Integrations against provider specific client APIs were removed in the 2.6 release in favor of litellm.

https://github.com/stanfordnlp/dspy/blob/main/docs/docs/roadmap.md#on-lms
0f4258f

@richardwhiteii
Copy link
Author

This is an alternate way at reusing access to a model in this case Claude without going through the api. This works for someone prototyping on their local development environment before using something like litellm against their API token count.

@okhat okhat closed this Aug 11, 2025
@okhat
Copy link
Collaborator

okhat commented Aug 11, 2025

Thank you @richardwhiteii , @beapirate ! @richardwhiteii Maybe this could be a separate helper package?

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.

3 participants