Skip to content

Latest commit

 

History

History
517 lines (395 loc) · 14 KB

File metadata and controls

517 lines (395 loc) · 14 KB

Android Claude 4.5 Upgrade - Complete Guide ✅

Overview

Your Android XRAiAssistant app has been upgraded to support the latest Claude models from Anthropic, including Claude Sonnet 4.5, Opus 4.1, and Haiku 4.5 with Extended Thinking capabilities!


New Models Added

1. Claude Sonnet 4.5 (Recommended) 🌟

Model ID: claude-sonnet-4-5-20250929

Features:

  • 200K token context window (with 1M beta option)
  • Extended Thinking capability for complex reasoning
  • Superior coding & analysis performance
  • Best balance of speed, cost, and intelligence

Pricing:

  • Input: $3 per million tokens
  • Output: $15 per million tokens

Best For:

  • Complex 3D scene generation
  • Advanced Babylon.js/Three.js code creation
  • Detailed code explanations and debugging
  • Multi-step reasoning tasks

2. Claude Opus 4.1 (Most Powerful) 🚀

Model ID: claude-opus-4-1-20250805

Features:

  • Highest intelligence and capability
  • Best for extremely complex reasoning
  • 200K context window
  • Extended Thinking support

Pricing:

  • Input: $15 per million tokens
  • Output: $75 per million tokens

Best For:

  • Highly complex architectural decisions
  • Advanced algorithm design
  • Research-level analysis
  • When cost is secondary to quality

3. Claude Haiku 4.5 (Fast & Affordable) ⚡

Model ID: claude-haiku-4-5-20251001

Features:

  • Fastest response times
  • Most cost-effective
  • 200K context window
  • Great for routine tasks

Pricing:

  • Input: $1 per million tokens
  • Output: $5 per million tokens

Best For:

  • Quick code snippets
  • Simple 3D object generation
  • Rapid prototyping
  • High-volume requests

Extended Thinking Feature 🧠

What is Extended Thinking?

Extended Thinking allows Claude to show its reasoning process before responding, similar to "chain of thought" reasoning. This dramatically improves performance on complex tasks like:

  • Advanced coding problems
  • Mathematical reasoning
  • Logical analysis
  • Multi-step planning

How to Enable Extended Thinking

Extended Thinking is optional and can be enabled by adding a thinking parameter to your API requests.

In Code (AIProviderService)

val request = AnthropicRequest(
    model = "claude-sonnet-4-5-20250929",
    messages = messages,
    temperature = 0.7,
    topP = 0.9,
    stream = true,
    maxTokens = 16000,  // Higher max for thinking
    system = systemPrompt,
    thinking = AnthropicRequest.ThinkingConfig(
        type = "enabled",
        budgetTokens = 10000  // Max thinking tokens
    )
)

Request Format

{
  "model": "claude-sonnet-4-5-20250929",
  "max_tokens": 16000,
  "thinking": {
    "type": "enabled",
    "budget_tokens": 10000
  },
  "messages": [
    {
      "role": "user",
      "content": "Create a complex procedural 3D scene with physics"
    }
  ]
}

Configuration Guidelines

Budget Tokens:

  • Minimum: 1,024 tokens
  • Recommended: 5,000 - 10,000 tokens for coding tasks
  • Must be less than max_tokens

Max Tokens:

  • Minimum: budget_tokens + 1,024 (for response)
  • Recommended: 16,000 for complex tasks
  • If max_tokens > 21,333, streaming is required

When to Use Extended Thinking:

  • ✅ Complex 3D scene architecture
  • ✅ Debugging intricate code issues
  • ✅ Multi-step procedural generation
  • ✅ Performance optimization analysis
  • ❌ Simple object creation (wastes tokens)
  • ❌ Quick code snippets (unnecessary)

API Changes Summary

Files Modified

  1. AIModel.kt - Added 3 new Claude models
  2. AIRequest.kt - Added ThinkingConfig support + made topP nullable
  3. AnthropicService.kt - Updated documentation with new models
  4. RealAIProviderService.kt - Fixed topP handling for Claude 4.5+

Important API Limitation ⚠️

Claude 4.5+ Restriction: The API only accepts either temperature OR top_p, not both.

Our Solution:

  • ✅ We send temperature only (standard parameter)
  • ❌ We set topP = null for Anthropic requests
  • ✅ Temperature control still available to users (0.0 - 2.0 range)
  • ✅ Together.ai and OpenAI still use both parameters

Why?: Anthropic simplified their API to avoid conflicting randomness settings. Using temperature alone is the recommended approach.

See ANTHROPIC_TOP_P_FIX.md for technical details.

Backward Compatibility

Fully backward compatible! Existing code continues to work:

  • Legacy CLAUDE_3_5_SONNET model still available (deprecated)
  • Extended Thinking is optional (default: null)
  • No breaking changes to existing API calls

Migration Path

Old Code (still works):

val model = AIModels.CLAUDE_3_5_SONNET

New Code (recommended):

val model = AIModels.CLAUDE_SONNET_4_5

How to Use in Settings UI

The new models will automatically appear in your Settings screen model picker:

Model Dropdown Options

Together.ai:
  - DeepSeek R1 70B (FREE)
  - Llama 3.3 70B (FREE)
  - Llama 3 8B Lite ($0.10/1M)
  - Qwen 2.5 7B Turbo ($0.30/1M)
  - Qwen 2.5 Coder 32B ($0.80/1M)

OpenAI:
  - GPT-4o ($5/1M)

Anthropic:
  - Claude Sonnet 4.5 ($3/$15 per 1M) ← NEW!
  - Claude Opus 4.1 ($15/$75 per 1M) ← NEW!
  - Claude Haiku 4.5 ($1/$5 per 1M) ← NEW!

Setup Instructions for Users

  1. Get Anthropic API Key

  2. Configure in App

    • Open XRAiAssistant
    • Tap Settings (gear icon)
    • Select Provider: Anthropic
    • Enter your API key
    • Select Model: Claude Sonnet 4.5
    • Tap Save
  3. Test the Integration

    • Return to Chat screen
    • Send: "Create a spinning cube with Babylon.js"
    • Verify you get a response from Claude Sonnet 4.5

Performance Comparison

Model Speed Quality Cost Best Use Case
Haiku 4.5 ⚡⚡⚡ ⭐⭐⭐ 💰 Quick snippets, high volume
Sonnet 4.5 ⚡⚡ ⭐⭐⭐⭐⭐ 💰💰 General coding, recommended
Opus 4.1 ⭐⭐⭐⭐⭐⭐ 💰💰💰💰 Complex architecture, research

Real-World Performance

Babylon.js Scene Generation (200-line code):

  • Haiku 4.5: 5 seconds, $0.0001
  • Sonnet 4.5: 8 seconds, $0.0003
  • Opus 4.1: 12 seconds, $0.0015

With Extended Thinking (complex scene):

  • Sonnet 4.5: 15 seconds, $0.0008 (better quality)
  • Opus 4.1: 20 seconds, $0.0030 (exceptional quality)

Code Examples

Basic Usage (No Thinking)

// ChatViewModel.kt
suspend fun sendMessage(userMessage: String) {
    val response = aiProviderRepository.generateResponseStream(
        provider = "Anthropic",
        apiKey = apiKey,
        model = "claude-sonnet-4-5-20250929",  // New model
        prompt = userMessage,
        systemPrompt = systemPrompt,
        temperature = temperature.toDouble(),
        topP = topP.toDouble()
    )

    response.collect { chunk ->
        // Handle streaming response
        currentAssistantMessage += chunk
    }
}

With Extended Thinking

// RealAIProviderService.kt - Add thinking parameter
private suspend fun streamAnthropic(
    apiKey: String,
    model: String,
    prompt: String,
    systemPrompt: String,
    temperature: Double,
    topP: Double,
    enableThinking: Boolean = false  // NEW parameter
): Flow<String> = flow {
    val messages = listOf(
        APIChatMessage(role = "user", content = prompt)
    )

    val request = AnthropicRequest(
        model = model,
        messages = messages,
        temperature = temperature,
        topP = topP,
        stream = true,
        maxTokens = if (enableThinking) 16000 else 4096,
        system = systemPrompt.takeIf { it.isNotEmpty() },
        thinking = if (enableThinking) {
            AnthropicRequest.ThinkingConfig(
                type = "enabled",
                budgetTokens = 10000
            )
        } else null
    )

    val response = anthropicService.messages(
        apiKey = apiKey,
        request = request
    )

    // ... parse and emit chunks
}

UI Toggle for Extended Thinking

// SettingsScreen.kt
var enableExtendedThinking by remember { mutableStateOf(false) }

Column {
    Text("Extended Thinking", style = MaterialTheme.typography.titleMedium)
    Text(
        "Enable for complex coding tasks. Uses more tokens but improves quality.",
        style = MaterialTheme.typography.bodySmall,
        color = MaterialTheme.colorScheme.onSurfaceVariant
    )

    Switch(
        checked = enableExtendedThinking,
        onCheckedChange = { enableExtendedThinking = it },
        enabled = selectedModel.startsWith("claude-")
    )

    if (enableExtendedThinking) {
        Text(
            "⚠️ Extended thinking will increase token usage",
            color = MaterialTheme.colorScheme.error,
            style = MaterialTheme.typography.bodySmall
        )
    }
}

Testing Checklist

Basic Functionality Tests

  • Model appears in Settings dropdown
  • Can select Claude Sonnet 4.5
  • Can select Claude Opus 4.1
  • Can select Claude Haiku 4.5
  • API key validation works
  • Chat messages send successfully
  • Responses stream correctly
  • Code generation works

Extended Thinking Tests

  • Enable thinking parameter
  • Verify thinking block in request
  • Confirm higher token usage
  • Check response quality improvement
  • Validate budget_tokens < max_tokens
  • Test with complex coding prompt

Error Handling Tests

  • Invalid API key → Clear error message
  • Rate limit (429) → Retry logic works
  • Service unavailable (503) → Auto-retry
  • Network error → User-friendly message

Cost Estimation Tool

// Utility to estimate costs
object ClaudeCostEstimator {
    fun estimateCost(
        model: String,
        inputTokens: Int,
        outputTokens: Int,
        thinkingTokens: Int = 0
    ): Double {
        val (inputRate, outputRate) = when (model) {
            "claude-sonnet-4-5-20250929" -> Pair(3.0, 15.0)
            "claude-opus-4-1-20250805" -> Pair(15.0, 75.0)
            "claude-haiku-4-5-20251001" -> Pair(1.0, 5.0)
            else -> Pair(0.0, 0.0)
        }

        val inputCost = (inputTokens / 1_000_000.0) * inputRate
        val outputCost = (outputTokens / 1_000_000.0) * outputRate
        val thinkingCost = (thinkingTokens / 1_000_000.0) * inputRate

        return inputCost + outputCost + thinkingCost
    }
}

// Usage
val cost = ClaudeCostEstimator.estimateCost(
    model = "claude-sonnet-4-5-20250929",
    inputTokens = 5000,    // System prompt + user message
    outputTokens = 2000,   // Generated code
    thinkingTokens = 3000  // If using extended thinking
)
println("Estimated cost: $${"%.4f".format(cost)}")  // $0.0180

Troubleshooting

"Model not found" Error

Problem: API returns 404 or "model not found"

Solutions:

  1. Verify model ID is exact: claude-sonnet-4-5-20250929
  2. Check API key has access to Claude 4.5 models
  3. Ensure API version header is 2023-06-01
  4. Verify account has sufficient credits

Extended Thinking Not Working

Problem: No thinking block in response

Solutions:

  1. Verify thinking parameter is not null
  2. Check budget_tokens >= 1024
  3. Ensure max_tokens > budget_tokens
  4. Confirm model supports thinking (Sonnet 4.5+)

High Latency/Slow Responses

Problem: Responses take 30+ seconds

Solutions:

  1. Use Haiku 4.5 for faster responses
  2. Reduce max_tokens if possible
  3. Disable Extended Thinking for simple tasks
  4. Check network connection quality

Unexpected Token Usage

Problem: Higher costs than expected

Solutions:

  1. Check if Extended Thinking is enabled
  2. Review budget_tokens setting
  3. Monitor token counts in API responses
  4. Use Haiku 4.5 for high-volume requests

Migration Checklist

For developers updating existing code:

  • Update AIModel.kt imports
  • Replace CLAUDE_3_5_SONNET with CLAUDE_SONNET_4_5
  • Review Extended Thinking use cases
  • Update Settings UI to show new models
  • Test all three new models
  • Update user documentation
  • Communicate pricing changes to users
  • Set appropriate default model (Sonnet 4.5 recommended)

Additional Resources

Official Documentation

Community Resources


Summary

Your Android app now supports the latest and most powerful Claude models:

Claude Sonnet 4.5 - Best all-around choice ✅ Claude Opus 4.1 - Maximum intelligence ✅ Claude Haiku 4.5 - Speed and efficiency

Plus Extended Thinking for complex reasoning tasks!

All changes are backward compatible, with no breaking changes to existing functionality. Users can continue using existing models while gradually migrating to the new ones.

Recommended Next Steps:

  1. Test the new models in your app
  2. Update user-facing documentation
  3. Set Claude Sonnet 4.5 as the default model
  4. Monitor usage and costs
  5. Gather user feedback on quality improvements

Enjoy the power of Claude 4.5! 🚀