Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
dde8c49a
authored
May 29, 2025
by
neotf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: enhance cache_create_tokens calculation for OpenRouter
parent
c0df3876
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
0 deletions
+29
-0
dto/openai_response.go
+2
-0
service/quota.go
+27
-0
No files found.
dto/openai_response.go
View file @
dde8c49a
...
@@ -178,6 +178,8 @@ type Usage struct {
...
@@ -178,6 +178,8 @@ type Usage struct {
InputTokens
int
`json:"input_tokens"`
InputTokens
int
`json:"input_tokens"`
OutputTokens
int
`json:"output_tokens"`
OutputTokens
int
`json:"output_tokens"`
InputTokensDetails
*
InputTokenDetails
`json:"input_tokens_details"`
InputTokensDetails
*
InputTokenDetails
`json:"input_tokens_details"`
// OpenRouter Params
Cost
float64
`json:"cost,omitempty"`
}
}
type
InputTokenDetails
struct
{
type
InputTokenDetails
struct
{
...
...
service/quota.go
View file @
dde8c49a
...
@@ -3,6 +3,7 @@ package service
...
@@ -3,6 +3,7 @@ package service
import
(
import
(
"errors"
"errors"
"fmt"
"fmt"
"math"
"one-api/common"
"one-api/common"
constant2
"one-api/constant"
constant2
"one-api/constant"
"one-api/dto"
"one-api/dto"
...
@@ -214,6 +215,11 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -214,6 +215,11 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
cacheCreationRatio
:=
priceData
.
CacheCreationRatio
cacheCreationRatio
:=
priceData
.
CacheCreationRatio
cacheCreationTokens
:=
usage
.
PromptTokensDetails
.
CachedCreationTokens
cacheCreationTokens
:=
usage
.
PromptTokensDetails
.
CachedCreationTokens
if
relayInfo
.
ChannelType
==
common
.
ChannelTypeOpenRouter
&&
priceData
.
CacheCreationRatio
!=
1
{
cacheCreationTokens
=
CalcOpenRouterCacheCreateTokens
(
*
usage
,
priceData
)
promptTokens
=
promptTokens
-
cacheCreationTokens
-
cacheTokens
}
calculateQuota
:=
0.0
calculateQuota
:=
0.0
if
!
priceData
.
UsePrice
{
if
!
priceData
.
UsePrice
{
calculateQuota
=
float64
(
promptTokens
)
calculateQuota
=
float64
(
promptTokens
)
...
@@ -261,6 +267,27 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -261,6 +267,27 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
tokenName
,
quota
,
logContent
,
relayInfo
.
TokenId
,
userQuota
,
int
(
useTimeSeconds
),
relayInfo
.
IsStream
,
relayInfo
.
Group
,
other
)
tokenName
,
quota
,
logContent
,
relayInfo
.
TokenId
,
userQuota
,
int
(
useTimeSeconds
),
relayInfo
.
IsStream
,
relayInfo
.
Group
,
other
)
}
}
func
CalcOpenRouterCacheCreateTokens
(
usage
dto
.
Usage
,
priceData
helper
.
PriceData
)
int
{
if
priceData
.
CacheCreationRatio
==
1
{
return
0
}
quotaPrice
:=
priceData
.
ModelRatio
/
common
.
QuotaPerUnit
promptCacheCreatePrice
:=
quotaPrice
*
priceData
.
CacheCreationRatio
promptCacheReadPrice
:=
quotaPrice
*
priceData
.
CacheRatio
completionPrice
:=
quotaPrice
*
priceData
.
CompletionRatio
cost
:=
usage
.
Cost
totalPromptTokens
:=
float64
(
usage
.
PromptTokens
)
completionTokens
:=
float64
(
usage
.
CompletionTokens
)
promptCacheReadTokens
:=
float64
(
usage
.
PromptTokensDetails
.
CachedTokens
)
return
int
(
math
.
Round
((
cost
-
totalPromptTokens
*
quotaPrice
+
promptCacheReadTokens
*
(
quotaPrice
-
promptCacheReadPrice
)
-
completionTokens
*
completionPrice
)
/
(
promptCacheCreatePrice
-
quotaPrice
)))
}
func
PostAudioConsumeQuota
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
,
func
PostAudioConsumeQuota
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
,
usage
*
dto
.
Usage
,
preConsumedQuota
int
,
userQuota
int
,
priceData
helper
.
PriceData
,
extraContent
string
)
{
usage
*
dto
.
Usage
,
preConsumedQuota
int
,
userQuota
int
,
priceData
helper
.
PriceData
,
extraContent
string
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment