Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-10-26
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Design: Claude Provider Modernization with Anthropic SDK

## Context
See `proposal.md` for motivation. `ClaudeProviderMixin` in `src/devops_cli/ai/client/claude.py` constructs HTTP requests manually. It uses obsolete model names, hard-coded model name string checks, and custom SSE decoders.

## Goals / Non-Goals

**Goals:**
- Delegate Claude message generation and streaming to the official Anthropic SDK (`anthropic.Anthropic`).
- Supply the factory-built HTTP client from `_shared_client()` to `anthropic.Anthropic(http_client=...)`.
- Resolve model capabilities via `client.models.retrieve(model)` with process-level caching.
- Emit adaptive thinking payloads (`thinking={"type": "adaptive"}`) with `output_config.effort` when supported.
- Emit identical sampling parameters across streamed and unstreamed requests only when supported.
- Enforce mandatory `ai.model` configuration for provider `claude`.

**Non-Goals:**
- Modify OpenAI, Copilot, or Ollama provider implementations.
- Introduce compatibility shims for obsolete Claude 3 models.

## Decisions

### Decision 1: Use official Anthropic SDK client
`ClaudeProviderMixin` constructs an `anthropic.Anthropic` client instance. It passes `http_client=self._shared_client()` and `max_retries=0`.

- *Rejected alternative*: Maintain hand-written Anthropic REST payloads and SSE decoding.
- *Evidence rejecting alternative*: Current models use adaptive thinking and reject obsolete `budget_tokens` and sampling parameters with HTTP 400. Hand-written protocol parsers violate rule 3 of `AGENTS.md`.
- *Rejected alternative*: Route Claude requests through Pydantic AI direct facades.
- *Evidence rejecting alternative*: Issue 1852 deleted dead Pydantic AI facades because `LLMClient` serves as the sole model seam.

### Decision 2: Dynamic capability discovery with process caching
Model capabilities are retrieved via `client.models.retrieve(model_name)` and cached in memory. Explicit `supports_reasoning` configuration overrides discovered thinking capability.

- *Rejected alternative*: Model name pattern matching (e.g. `claude-3-7` or `:thinking`).
- *Evidence rejecting alternative*: Anthropic retired Claude 3.5 and 3.7. Static name checks fail on modern models like Claude Opus 4.7 and Claude Sonnet 5.

### Decision 3: Mandatory model configuration
Empty or unset `ai.model` configuration raises a `ConfigurationError` naming `ai.model`.

- *Rejected alternative*: Retain fallback default model `claude-3-5-sonnet`.
- *Evidence rejecting alternative*: Anthropic retired `claude-3-5-sonnet`. Falling back to retired models causes runtime failures.

## Risks / Trade-offs

- [Models API overhead] → The client caches capability lookups in-memory per model name for the process lifetime.
- [SDK exception wrapping] → Catch `anthropic.APIConnectionError` and `anthropic.APIStatusError` and map them through `self._connection_error` and `self._provider_http_error`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Proposal: Claude Provider Modernization with Anthropic SDK

## Why
This change resolves #1428. The Claude provider constructs HTTP requests manually and uses retired models, obsolete thinking payload schemas, and incompatible sampling options. Current Anthropic models reject these requests with HTTP 400.

## What Changes
- Replace manual HTTP client requests in `ClaudeProviderMixin` with official `anthropic.Anthropic` client calls.
- Enforce mandatory `ai.model` configuration for provider `claude`, raising a `ConfigurationError` when empty.
- Remove hard-coded model name heuristics (`claude-3-*`, `:thinking`) and retired `budget_tokens` parameters.
- Query and cache model capabilities via the Models API (`client.models.retrieve`), allowing `supports_reasoning` configuration overrides.
- Transmit adaptive thinking payloads (`{"type": "adaptive"}`) with `output_config.effort` when supported by model capabilities.
- Ensure consistent `temperature` and `top_p` sampling transmission across streamed and unstreamed requests only when the model supports sampling.
- Delete obsolete event framing functions `_CLAUDE_EVENT_FRAMES` and `_claude_stream_frame`.
- Add `anthropic==1.12.1` to project dependencies in `pyproject.toml`.

This change fits one pull request.

## Capabilities

### New Capabilities
None.

### Modified Capabilities
- `ai-model-sampling`: add requirements for Claude provider capability resolution, adaptive thinking options, and model presence enforcement.

## Impact
- `src/devops_cli/ai/client/claude.py`: modernized to use Anthropic SDK for messages and streaming.
- `src/devops_cli/ai/client/streaming.py`: removed obsolete Claude SSE event decoders.
- `src/devops_cli/ai/client/unified.py`: enforced non-empty `ai.model` validation for Claude provider.
- `pyproject.toml`: declared `anthropic==1.12.1` direct dependency.
- `openspec/specs/ai-model-sampling/spec.md`: updated specification with capability delta.

## Feasibility
The premise was confirmed against the live runtime environment. The `anthropic` SDK version 1.12.1 is present in `uv.lock`. Inspection of `anthropic.Anthropic.__init__` confirmed support for `http_client` and `max_retries=0`. Direct testing with `httpx2.MockTransport` confirmed that `client.models.retrieve` returns model capabilities and `client.messages.create` and `client.messages.stream` correctly format adaptive thinking and extra body parameters.

## Existing Tool Considered
The official `anthropic` Python SDK (version 1.12.1) is used directly rather than maintaining bespoke Anthropic REST and SSE decoders. This change deletes manual header generation, payload builders, and SSE frame parsers in `src/devops_cli/ai/client/claude.py` and `src/devops_cli/ai/client/streaming.py`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Spec Delta: ai-model-sampling

## ADDED Requirements

### Requirement: Mandatory model configuration for Claude provider
The system SHALL require a non-empty `ai.model` when provider `claude` is configured. If `ai.model` is empty or unset, initialization SHALL raise a `ConfigurationError` naming `ai.model`.

#### Scenario: Empty model configuration fails for Claude
- **WHEN** provider `claude` is initialized with an empty or unset `ai.model`
- **THEN** initialization raises a `ConfigurationError` naming `ai.model`

### Requirement: Claude provider capability-driven thinking
The system SHALL query model capabilities from the Anthropic Models API or local configuration overrides (`supports_reasoning`). When adaptive thinking is supported and enabled, requests SHALL carry `thinking` with `type: "adaptive"` and `output_config.effort`, and SHALL omit `budget_tokens`.

#### Scenario: Model with adaptive thinking receives adaptive thinking payload
- **WHEN** a Claude model declares adaptive thinking capability and thinking is enabled
- **THEN** outbound request payloads contain `thinking` with `type: "adaptive"` and `output_config.effort`, and omit `budget_tokens`

#### Scenario: Model without thinking capability receives standard payload
- **WHEN** a Claude model does not declare adaptive thinking capability or thinking is disabled
- **THEN** outbound request payloads omit `thinking` and `output_config`

### Requirement: Claude provider capability-driven sampling parity
The system SHALL send `temperature` and `top_p` on both streamed and unstreamed Claude requests only when the model declares sampling support, omitting them when sampling is unsupported, when adaptive thinking is active, or when `use_model_sampling` is enabled.

#### Scenario: Claude model declaring sampling support transmits identical parameters
- **WHEN** a Claude model declares sampling support and adaptive thinking is disabled
- **THEN** streamed and unstreamed requests carry identical `temperature` and `top_p` values

#### Scenario: Claude model without sampling support omits sampling parameters
- **WHEN** a Claude model does not declare sampling support
- **THEN** streamed and unstreamed requests omit `temperature` and `top_p`
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Tasks

## 1. Dependencies and Model Presence Validation

- [x] 1.1 Write failing test asserting provider `claude` requires non-empty `ai.model` and raises `ConfigurationError` naming `ai.model` (`uv run devops ci test tests/test_claude_provider.py`)
- [x] 1.2 Add `anthropic==1.12.1` to `pyproject.toml` dependencies and enforce mandatory `ai.model` in `LLMClient._validate_resolved_config` (`uv run devops ci test tests/test_claude_provider.py`)
- [x] 1.3 Regenerate command documentation (`uv run devops docs generate --sync-readme`)

## 2. Capability Resolution and Adaptive Thinking Payloads

- [x] 2.1 Write failing tests in `tests/test_claude_provider.py` asserting capability retrieval via Models API, adaptive thinking payload generation without `budget_tokens`, and SDK client HTTP client identity (`uv run devops ci test tests/test_claude_provider.py`)
- [x] 2.2 Rebuild `ClaudeProviderMixin._claude_messages` on `anthropic.Anthropic` using `_shared_client()`, resolve model capabilities via Models API, format adaptive thinking with `output_config.effort`, and remove `budget_tokens` and name checks (`uv run devops ci test tests/test_claude_provider.py`)
- [x] 2.3 Regenerate command documentation (`uv run devops docs generate --sync-readme`)

## 3. Sampling Parity, Stream Modernization, and Cleanup

- [x] 3.1 Write failing tests in `tests/test_claude_provider.py` asserting sampling parameter parity between streamed and unstreamed requests when supported, and omission when unsupported (`uv run devops ci test tests/test_claude_provider.py`)
- [x] 3.2 Rebuild `_claude_stream` via `anthropic.Anthropic.messages.stream`, delete `_CLAUDE_EVENT_FRAMES` and `_claude_stream_frame` in `src/devops_cli/ai/client/streaming.py`, purge `claude-3-` mentions across `src`, and update obsolete test references (`uv run devops ci test tests/test_claude_provider.py tests/test_ai_streaming.py`)
- [x] 3.3 Validate the complete quality gate and static checks (`uv run devops ci`)

## Workflow follow-up

- Pending a person: `devops ai chat` with provider `claude` and a current model returns a reply.
- Pending a person: review the diff in tuicr (`tuicr -r origin/main..HEAD`).
29 changes: 29 additions & 0 deletions openspec/specs/ai-model-sampling/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,32 @@ The system SHALL include `temperature`, `top_p`, `reasoning_effort`, and reply c
#### Scenario: Cache keys partition different temperatures
- **WHEN** two requests with identical messages are dispatched with differing temperatures
- **THEN** each request produces a distinct cache key and dispatches to the model

### Requirement: Mandatory model configuration for Claude provider
The system SHALL require a non-empty `ai.model` when provider `claude` is configured. If `ai.model` is empty or unset, initialization SHALL raise a `ConfigurationError` naming `ai.model`.

#### Scenario: Empty model configuration fails for Claude
- **WHEN** provider `claude` is initialized with an empty or unset `ai.model`
- **THEN** initialization raises a `ConfigurationError` naming `ai.model`

### Requirement: Claude provider capability-driven thinking
The system SHALL query model capabilities from the Anthropic Models API or local configuration overrides (`supports_reasoning`). When adaptive thinking is supported and enabled, requests SHALL carry `thinking` with `type: "adaptive"` and `output_config.effort`, and SHALL omit `budget_tokens`.

#### Scenario: Model with adaptive thinking receives adaptive thinking payload
- **WHEN** a Claude model declares adaptive thinking capability and thinking is enabled
- **THEN** outbound request payloads contain `thinking` with `type: "adaptive"` and `output_config.effort`, and omit `budget_tokens`

#### Scenario: Model without thinking capability receives standard payload
- **WHEN** a Claude model does not declare adaptive thinking capability or thinking is disabled
- **THEN** outbound request payloads omit `thinking` and `output_config`

### Requirement: Claude provider capability-driven sampling parity
The system SHALL send `temperature` and `top_p` on both streamed and unstreamed Claude requests only when the model declares sampling support, omitting them when sampling is unsupported, when adaptive thinking is active, or when `use_model_sampling` is enabled.

#### Scenario: Claude model declaring sampling support transmits identical parameters
- **WHEN** a Claude model declares sampling support and adaptive thinking is disabled
- **THEN** streamed and unstreamed requests carry identical `temperature` and `top_p` values

#### Scenario: Claude model without sampling support omits sampling parameters
- **WHEN** a Claude model does not declare sampling support
- **THEN** streamed and unstreamed requests omit `temperature` and `top_p`
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies = [
"logfire==5.2.0",
"pynacl==1.6.2",
"openai==3.26.1",
"anthropic==1.12.1",
"opentelemetry-api==1.45.1",
"opentelemetry-semantic-conventions==0.66b1",
"secretstorage==3.5.0; sys_platform == 'linux'",
Expand Down
Loading
Loading