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
877529ef
authored
Jun 22, 2025
by
Calcium-Ion
Committed by
GitHub
Jun 22, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1120 from neotf/feat-04
feat: enhance token usage details for upstream OpenRouter
parents
2dee2300
984152c7
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
47 additions
and
3 deletions
+47
-3
dto/openai_request.go
+1
-0
dto/openai_response.go
+2
-0
relay/channel/openai/adaptor.go
+5
-0
service/convert.go
+6
-3
service/quota.go
+33
-0
No files found.
dto/openai_request.go
View file @
877529ef
...
@@ -57,6 +57,7 @@ type GeneralOpenAIRequest struct {
...
@@ -57,6 +57,7 @@ type GeneralOpenAIRequest struct {
ExtraBody
json
.
RawMessage
`json:"extra_body,omitempty"`
ExtraBody
json
.
RawMessage
`json:"extra_body,omitempty"`
WebSearchOptions
*
WebSearchOptions
`json:"web_search_options,omitempty"`
WebSearchOptions
*
WebSearchOptions
`json:"web_search_options,omitempty"`
// OpenRouter Params
// OpenRouter Params
Usage
json
.
RawMessage
`json:"usage,omitempty"`
Reasoning
json
.
RawMessage
`json:"reasoning,omitempty"`
Reasoning
json
.
RawMessage
`json:"reasoning,omitempty"`
// Ali Qwen Params
// Ali Qwen Params
VlHighResolutionImages
json
.
RawMessage
`json:"vl_high_resolution_images,omitempty"`
VlHighResolutionImages
json
.
RawMessage
`json:"vl_high_resolution_images,omitempty"`
...
...
dto/openai_response.go
View file @
877529ef
...
@@ -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
{
...
...
relay/channel/openai/adaptor.go
View file @
877529ef
...
@@ -159,6 +159,11 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
...
@@ -159,6 +159,11 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
if
info
.
ChannelType
!=
common
.
ChannelTypeOpenAI
&&
info
.
ChannelType
!=
common
.
ChannelTypeAzure
{
if
info
.
ChannelType
!=
common
.
ChannelTypeOpenAI
&&
info
.
ChannelType
!=
common
.
ChannelTypeAzure
{
request
.
StreamOptions
=
nil
request
.
StreamOptions
=
nil
}
}
if
info
.
ChannelType
==
common
.
ChannelTypeOpenRouter
{
if
len
(
request
.
Usage
)
==
0
{
request
.
Usage
=
json
.
RawMessage
(
`{"include":true}`
)
}
}
if
strings
.
HasPrefix
(
request
.
Model
,
"o"
)
{
if
strings
.
HasPrefix
(
request
.
Model
,
"o"
)
{
if
request
.
MaxCompletionTokens
==
0
&&
request
.
MaxTokens
!=
0
{
if
request
.
MaxCompletionTokens
==
0
&&
request
.
MaxTokens
!=
0
{
request
.
MaxCompletionTokens
=
request
.
MaxTokens
request
.
MaxCompletionTokens
=
request
.
MaxTokens
...
...
service/convert.go
View file @
877529ef
...
@@ -276,12 +276,15 @@ func StreamResponseOpenAI2Claude(openAIResponse *dto.ChatCompletionsStreamRespon
...
@@ -276,12 +276,15 @@ func StreamResponseOpenAI2Claude(openAIResponse *dto.ChatCompletionsStreamRespon
}
}
if
info
.
Done
{
if
info
.
Done
{
claudeResponses
=
append
(
claudeResponses
,
generateStopBlock
(
info
.
ClaudeConvertInfo
.
Index
))
claudeResponses
=
append
(
claudeResponses
,
generateStopBlock
(
info
.
ClaudeConvertInfo
.
Index
))
if
info
.
ClaudeConvertInfo
.
Usage
!=
nil
{
oaiUsage
:=
info
.
ClaudeConvertInfo
.
Usage
if
oaiUsage
!=
nil
{
claudeResponses
=
append
(
claudeResponses
,
&
dto
.
ClaudeResponse
{
claudeResponses
=
append
(
claudeResponses
,
&
dto
.
ClaudeResponse
{
Type
:
"message_delta"
,
Type
:
"message_delta"
,
Usage
:
&
dto
.
ClaudeUsage
{
Usage
:
&
dto
.
ClaudeUsage
{
InputTokens
:
info
.
ClaudeConvertInfo
.
Usage
.
PromptTokens
,
InputTokens
:
oaiUsage
.
PromptTokens
,
OutputTokens
:
info
.
ClaudeConvertInfo
.
Usage
.
CompletionTokens
,
OutputTokens
:
oaiUsage
.
CompletionTokens
,
CacheCreationInputTokens
:
oaiUsage
.
PromptTokensDetails
.
CachedCreationTokens
,
CacheReadInputTokens
:
oaiUsage
.
PromptTokensDetails
.
CachedTokens
,
},
},
Delta
:
&
dto
.
ClaudeMediaMessage
{
Delta
:
&
dto
.
ClaudeMediaMessage
{
StopReason
:
common
.
GetPointer
[
string
](
stopReasonOpenAI2Claude
(
info
.
FinishReason
)),
StopReason
:
common
.
GetPointer
[
string
](
stopReasonOpenAI2Claude
(
info
.
FinishReason
)),
...
...
service/quota.go
View file @
877529ef
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"errors"
"errors"
"fmt"
"fmt"
"log"
"log"
"math"
"one-api/common"
"one-api/common"
constant2
"one-api/constant"
constant2
"one-api/constant"
"one-api/dto"
"one-api/dto"
...
@@ -231,6 +232,17 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -231,6 +232,17 @@ 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
{
promptTokens
-=
cacheTokens
if
cacheCreationTokens
==
0
&&
priceData
.
CacheCreationRatio
!=
1
&&
usage
.
Cost
!=
0
{
maybeCacheCreationTokens
:=
CalcOpenRouterCacheCreateTokens
(
*
usage
,
priceData
)
if
promptTokens
>=
maybeCacheCreationTokens
{
cacheCreationTokens
=
maybeCacheCreationTokens
}
}
promptTokens
-=
cacheCreationTokens
}
calculateQuota
:=
0.0
calculateQuota
:=
0.0
if
!
priceData
.
UsePrice
{
if
!
priceData
.
UsePrice
{
calculateQuota
=
float64
(
promptTokens
)
calculateQuota
=
float64
(
promptTokens
)
...
@@ -278,6 +290,27 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -278,6 +290,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