mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
prompt_tokens meant two things. The family and relay paths recorded the part of the prompt read fresh, the table path and the providers the whole prompt, so a cached enso-auto row read cache_read > prompt. No path recorded a cache write: Anthropic's cache_creation_input_tokens was folded into the fresh read and billed at the input rate, though a write is priced above it, and OpenRouter's cache_write_tokens was not read at all. cache_write_tokens was 0 on every row. A record's prompt is now every input token, with the cache reads and writes parts of it, whatever shape the upstream reported: OpenAI's cached_tokens inside prompt_tokens, OpenRouter's cache_write_tokens beside them, Anthropic's two counts beside input_tokens. Pricing bills the input rate on the part read fresh and each cache part at its own rate: a family SKU's cache_read and cache_write (a resold model's input_cache_read and input_cache_write plus the vendor's fee), else the input rate. The Anthropic provider and the OpenAI adapter over Anthropic now read both cache counts, and the providers' settles pass them.
203 lines
7.3 KiB
Go
203 lines
7.3 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/anthropics/anthropic-sdk-go"
|
|
"github.com/anthropics/anthropic-sdk-go/option"
|
|
"github.com/hanzoai/ai/i18n"
|
|
"github.com/hanzoai/ai/proxy"
|
|
)
|
|
|
|
type AnthropicModelProvider struct {
|
|
subType string
|
|
secretKey string
|
|
budgetTokens int
|
|
enableThinking bool
|
|
}
|
|
|
|
func NewAnthropicModelProvider(subType string, secretKey string, enableThinking bool, budgetTokens int) (*AnthropicModelProvider, error) {
|
|
return &AnthropicModelProvider{subType: subType, secretKey: secretKey, enableThinking: enableThinking, budgetTokens: budgetTokens}, nil
|
|
}
|
|
|
|
func (p *AnthropicModelProvider) GetPricing() string {
|
|
return `URL:
|
|
https://docs.anthropic.com/en/docs/about-claude/pricing
|
|
|
|
| Model family | Context window | Input Pricing | Output Pricing |
|
|
|---------------------|----------------|-----------------------|-----------------------|
|
|
| Claude Opus 4.5 | 200,000 tokens | $5.00/million tokens | $25.00/million tokens |
|
|
| Claude Opus 4.1 | 200,000 tokens | $15.00/million tokens | $75.00/million tokens |
|
|
| Claude Opus 4 | 200,000 tokens | $15.00/million tokens | $75.00/million tokens |
|
|
| Claude Sonnet 4 | 200,000 tokens | $3.00/million tokens | $15.00/million tokens |
|
|
| Claude Sonnet 3.7 | 200,000 tokens | $3.00/million tokens | $15.00/million tokens |
|
|
| Claude Sonnet 3.5 | 200,000 tokens | $3.00/million tokens | $15.00/million tokens |
|
|
| Claude Haiku 3.5 | 200,000 tokens | $0.80/million tokens | $4.00/million tokens |
|
|
| Claude Opus 3 | 200,000 tokens | $15.00/million tokens | $75.00/million tokens |
|
|
| Claude Haiku 3 | 200,000 tokens | $0.25/million tokens | $1.25/million tokens |
|
|
`
|
|
}
|
|
|
|
func (p *AnthropicModelProvider) calculatePrice(modelResult *ModelResult, lang string) error {
|
|
var inputPricePerThousandTokens, outputPricePerThousandTokens float64
|
|
priceTable := map[string][]float64{
|
|
"claude-opus-4-5": {0.005, 0.025},
|
|
"claude-opus-4-1": {0.015, 0.075},
|
|
"claude-opus-4-0": {0.015, 0.075},
|
|
"claude-opus-4-20250514": {0.015, 0.075},
|
|
"claude-4-opus-20250514": {0.015, 0.075},
|
|
"claude-sonnet-4-0": {0.003, 0.015},
|
|
"claude-sonnet-4-20250514": {0.003, 0.015},
|
|
"claude-4-sonnet-20250514": {0.003, 0.015},
|
|
"claude-3-7-sonnet-latest": {0.003, 0.015},
|
|
"claude-3-7-sonnet-20250219": {0.003, 0.015},
|
|
"claude-3-5-haiku-latest": {0.0008, 0.004},
|
|
"claude-3-5-haiku-20241022": {0.0008, 0.004},
|
|
"claude-3-5-sonnet-latest": {0.003, 0.015},
|
|
"claude-3-opus-latest": {0.015, 0.075},
|
|
"claude-3-haiku-20240307": {0.00025, 0.00125},
|
|
}
|
|
|
|
if priceItem, ok := priceTable[p.subType]; ok {
|
|
inputPricePerThousandTokens = priceItem[0]
|
|
outputPricePerThousandTokens = priceItem[1]
|
|
} else {
|
|
return fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "embedding:calculatePrice() error: unknown model type: %s"), p.subType))
|
|
}
|
|
|
|
inputPrice := getPrice(modelResult.PromptTokenCount, inputPricePerThousandTokens)
|
|
outputPrice := getPrice(modelResult.ResponseTokenCount, outputPricePerThousandTokens)
|
|
modelResult.TotalPrice = AddPrices(inputPrice, outputPrice)
|
|
modelResult.Currency = "USD"
|
|
return nil
|
|
}
|
|
|
|
func (p *AnthropicModelProvider) QueryText(question string, writer io.Writer, history []*RawMessage, prompt string, knowledgeMessages []*RawMessage, agentInfo *AgentInfo, lang string) (*ModelResult, error) {
|
|
client := anthropic.NewClient(
|
|
option.WithAPIKey(p.secretKey),
|
|
option.WithHTTPClient(proxy.ProxyHttpClient),
|
|
)
|
|
|
|
if strings.HasPrefix(question, "$CloudDryRun$") {
|
|
modelResult, err := getDefaultModelResult(p.subType, question, "")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s", i18n.Translate(lang, "model:cannot calculate tokens"))
|
|
}
|
|
if GetContextLength(p.subType) > modelResult.TotalTokenCount {
|
|
return modelResult, nil
|
|
} else {
|
|
return nil, fmt.Errorf("%s", i18n.Translate(lang, "model:exceed max tokens"))
|
|
}
|
|
}
|
|
|
|
maxTokens := GetContextLength(p.subType)
|
|
|
|
var textBlockList []anthropic.TextBlockParam
|
|
systemMessages := getSystemMessages(prompt, knowledgeMessages)
|
|
for _, systemMessage := range systemMessages {
|
|
textBlockList = append(textBlockList, anthropic.TextBlockParam{
|
|
Text: systemMessage.Text,
|
|
Type: "text",
|
|
})
|
|
}
|
|
|
|
messages := []anthropic.MessageParam{}
|
|
for _, historyMessage := range slices.Backward(history) {
|
|
|
|
messages = append(messages, anthropic.NewAssistantMessage(anthropic.NewTextBlock(historyMessage.Text)))
|
|
}
|
|
messages = append(messages, anthropic.NewUserMessage(anthropic.NewTextBlock(question)))
|
|
|
|
messageParams := anthropic.MessageNewParams{
|
|
MaxTokens: int64(maxTokens),
|
|
Messages: messages,
|
|
Model: anthropic.Model(p.subType),
|
|
StopSequences: []string{"```\n"},
|
|
System: textBlockList,
|
|
}
|
|
if p.enableThinking {
|
|
messageParams.Thinking = anthropic.ThinkingConfigParamUnion{
|
|
OfEnabled: &anthropic.ThinkingConfigEnabledParam{
|
|
BudgetTokens: int64(p.budgetTokens),
|
|
},
|
|
}
|
|
}
|
|
stream := client.Messages.NewStreaming(context.TODO(), messageParams)
|
|
|
|
flusher, ok := writer.(http.Flusher)
|
|
if !ok {
|
|
return nil, fmt.Errorf("%s", i18n.Translate(lang, "model:writer does not implement http.Flusher"))
|
|
}
|
|
|
|
flushData := func(event string, data string) error {
|
|
if _, err := fmt.Fprintf(writer, "event: %s\ndata: %s\n\n", event, data); err != nil {
|
|
return err
|
|
}
|
|
flusher.Flush()
|
|
return nil
|
|
}
|
|
|
|
modelResult := &ModelResult{}
|
|
for stream.Next() {
|
|
event := stream.Current()
|
|
|
|
switch eventVariant := event.AsAny().(type) {
|
|
case anthropic.MessageStartEvent:
|
|
// The prompt as every record counts it: input_tokens is the part read
|
|
// fresh, and the cache reads and writes are beside it, not inside it.
|
|
u := eventVariant.Message.Usage
|
|
modelResult.CacheReadTokenCount = int(u.CacheReadInputTokens)
|
|
modelResult.CacheWriteTokenCount = int(u.CacheCreationInputTokens)
|
|
modelResult.PromptTokenCount = int(u.InputTokens) + modelResult.CacheReadTokenCount + modelResult.CacheWriteTokenCount
|
|
case anthropic.ContentBlockDeltaEvent:
|
|
switch deltaVariant := eventVariant.Delta.AsAny().(type) {
|
|
case anthropic.ThinkingDelta:
|
|
err := flushData("reason", deltaVariant.Thinking)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
case anthropic.TextDelta:
|
|
err := flushData("message", deltaVariant.Text)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
case anthropic.MessageDeltaEvent:
|
|
outputTokens := int(eventVariant.Usage.OutputTokens)
|
|
modelResult.ResponseTokenCount = outputTokens
|
|
}
|
|
}
|
|
|
|
if stream.Err() != nil {
|
|
return nil, stream.Err()
|
|
}
|
|
modelResult.TotalTokenCount = modelResult.PromptTokenCount + modelResult.ResponseTokenCount
|
|
|
|
err := p.calculatePrice(modelResult, lang)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return modelResult, nil
|
|
}
|