Skip to content

Conversation

@roomote
Copy link

@roomote roomote bot commented Nov 7, 2025

Summary

This PR implements support for custom model names in the Anthropic adapter, enabling third-party Anthropic-compatible providers and newer models not yet in the hardcoded list.

Problem

As reported in #9093, the Anthropic adapter currently has hardcoded model names and doesn't allow custom model identifiers such as:

  • Third-party models: MinMax-M2
  • Newer Claude models: claude-haiku-4-5, claude-sonnet-4-5-20250929
  • Custom Anthropic-compatible providers

Users had to manually edit the code to add new models, which is not sustainable.

Solution

This PR modifies the Anthropic adapter to:

  • ✅ Accept any string as a model ID while maintaining type safety for known models
  • ✅ Provide sensible defaults for unknown models (8192 max tokens, 200K context, images + caching support)
  • ✅ Enable prompt caching for custom models that support it
  • ✅ Maintain full backward compatibility with existing code

Changes

  1. Type system updates - Modified AnthropicModelId type to accept custom strings
  2. getModel() enhancements - Added logic to handle unknown models with appropriate defaults
  3. createMessage() improvements - Support for custom models with prompt caching
  4. Comprehensive test coverage - Added 8 new test cases covering various custom model scenarios

Testing

  • All existing tests pass ✅
  • New tests added for custom model support ✅
  • Type checking passes ✅
  • Linting passes ✅

Default Assumptions for Custom Models

Custom models receive these defaults:

  • Max tokens: 8192
  • Context window: 200,000
  • Images support: enabled
  • Prompt caching: enabled

These are conservative defaults that match modern Anthropic capabilities and will gracefully degrade if not supported.

Fixes #9093


Important

Adds support for custom Anthropic-compatible models by allowing any string as a model ID and providing sensible defaults for unknown models.

  • Behavior:
    • AnthropicModelId type now accepts custom strings in anthropic.ts.
    • getModel() in anthropic.ts provides defaults for unknown models (8192 max tokens, 200K context, images + caching support).
    • createMessage() in anthropic.ts supports custom models with prompt caching.
  • Testing:
    • Added 8 new test cases in anthropic.spec.ts for custom model scenarios.
    • All existing tests pass.
  • Misc:

This description was created by Ellipsis for fbb95f0. You can customize this summary. It will automatically update as commits are pushed.

- Allow custom model IDs in AnthropicModelId type
- Update AnthropicHandler.getModel() to handle unknown models with sensible defaults
- Modify createMessage() to support custom models with prompt caching
- Add comprehensive tests for custom model support
- Enables third-party Anthropic-compatible providers (MinMax, etc.)
- Fixes #9093
@roomote roomote bot requested review from cte, jr and mrubens as code owners November 7, 2025 04:33
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 7, 2025
@roomote
Copy link
Author

roomote bot commented Nov 7, 2025

Rooviewer Clock   See task on Roo Cloud

Review complete. Found 1 maintainability improvement opportunity.

  • Extract duplicated cache control logic into a helper function (lines 82-113 and 142-181)

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@dosubot dosubot bot added the enhancement New feature or request label Nov 7, 2025
Comment on lines +142 to +181
default: {
// Custom model with prompt caching support
const userMsgIndices = messages.reduce(
(acc, msg, index) => (msg.role === "user" ? [...acc, index] : acc),
[] as number[],
)

const lastUserMsgIndex = userMsgIndices[userMsgIndices.length - 1] ?? -1
const secondLastMsgUserIndex = userMsgIndices[userMsgIndices.length - 2] ?? -1

stream = await this.client.messages.create(
{
model: modelId,
max_tokens: maxTokens ?? ANTHROPIC_DEFAULT_MAX_TOKENS,
temperature,
thinking,
// Setting cache breakpoint for system prompt so new tasks can reuse it.
system: [{ text: systemPrompt, type: "text", cache_control: cacheControl }],
messages: messages.map((message, index) => {
if (index === lastUserMsgIndex || index === secondLastMsgUserIndex) {
return {
...message,
content:
typeof message.content === "string"
? [{ type: "text", text: message.content, cache_control: cacheControl }]
: message.content.map((content, contentIndex) =>
contentIndex === message.content.length - 1
? { ...content, cache_control: cacheControl }
: content,
),
}
}
return message
}),
stream: true,
},
{ headers: { "anthropic-beta": "prompt-caching-2024-07-31" } },
)
break
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache control logic (finding user message indices, applying cache control to last and second-to-last user messages) is duplicated between the known models case (lines 82-113) and this custom models default case. This duplication makes maintenance harder - future changes to the caching strategy would need to be applied in two places. Consider extracting this logic into a helper function that both branches can call.

Fix it with Roo Code or mention @roomote and request a fix.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

[ENHANCEMENT] Anthropic compatible api support

3 participants