Skip to content

AgentLM

Online: https://thekevinscott.github.io/coaxer/api/agent-lm/

coaxer.AgentLM

Bases: BaseLM

DSPy language model that routes calls through the Anthropic Agent SDK.

Each call to forward() spawns a Claude Code subprocess via claude_agent_sdk.query(). All keyword arguments (both constructor and per-call) are forwarded to ClaudeAgentOptions, giving full control over tools, turns, environment, and other SDK options.

For classification or structured output where you don't want the model to use filesystem tools, pass tools=[] and max_turns=20::

lm = AgentLM(tools=[])

The model identifier (default "claude-agent-sdk") is for DSPy's internal tracking only -- the actual model is determined by the Claude Code CLI.

Per-call kwargs override constructor kwargs (shallow merge).

forward(prompt=None, messages=None, **kwargs)

Synchronous forward pass. Bridges to the async SDK via run_sync.

Accepts either a plain string prompt or an OpenAI-style message list. Any extra kwargs are merged with constructor kwargs and forwarded to ClaudeAgentOptions.

aforward(prompt=None, messages=None, **kwargs) async

Async forward pass. Calls the SDK directly without threading.

copy(**kwargs)

Return a copy with updated kwargs.

update_history(entry)

Append an entry to the history.

inspect_history(n=1)

Return the last n history entries.

Constructor Parameters

Parameter Type Default Description
model str "claude-agent-sdk" Model identifier for DSPy tracking. The actual model is determined by the Claude Code CLI.
model_type str "chat" DSPy model type.
max_tokens int 4096 Maximum tokens (forwarded to DSPy, not the SDK).
**kwargs All other kwargs forwarded to ClaudeAgentOptions.

Common ClaudeAgentOptions

Parameter Type Description
tools list Tools available to the model. Pass [] for classification/structured output.
allowed_tools list[str] Allowlist of tool names (e.g., ["Read", "Glob"]).
disallowed_tools list[str] Denylist of tool names.
max_turns int Maximum agentic turns.
env dict[str, str] Environment variables for the SDK subprocess.

Methods

forward(prompt, messages, **kwargs)

Synchronous forward pass. Accepts either a plain string prompt or an OpenAI-style messages list. Per-call kwargs override constructor kwargs.

Returns a CompletionResponse (OpenAI-compatible format).

aforward(prompt, messages, **kwargs)

Async version of forward(). Calls the SDK directly without threading.

copy(**kwargs)

Returns a new AgentLM with updated kwargs. Useful for creating variants with different tool configurations.

inspect_history(n=1)

Returns the last n history entries (prompt + response pairs).

Examples

import dspy
from coaxer import AgentLM

# Basic usage
lm = AgentLM()
dspy.configure(lm=lm)

# Classification (no tools)
lm = AgentLM(tools=[])