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
ff29900f
authored
Mar 20, 2026
by
wenyifan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Add support for counting cache-hit tokens in llama.cpp OpenAI-Compatible API
parent
ed6ff0f2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
0 deletions
+24
-0
relay/channel/openai/relay-openai.go
+24
-0
No files found.
relay/channel/openai/relay-openai.go
View file @
ff29900f
...
...
@@ -627,6 +627,12 @@ func applyUsagePostProcessing(info *relaycommon.RelayInfo, usage *dto.Usage, res
usage
.
PromptTokensDetails
.
CachedTokens
=
usage
.
PromptCacheHitTokens
}
}
case
constant
.
ChannelTypeOpenAI
:
if
usage
.
PromptTokensDetails
.
CachedTokens
==
0
{
if
cachedTokens
,
ok
:=
extractLlamaCachedTokensFromBody
(
responseBody
);
ok
{
usage
.
PromptTokensDetails
.
CachedTokens
=
cachedTokens
}
}
}
}
...
...
@@ -689,3 +695,21 @@ func extractMoonshotCachedTokensFromBody(body []byte) (int, bool) {
return
0
,
false
}
// extractLlamaCachedTokensFromBody 从llama.cpp的非标准位置提取cache_n
func
extractLlamaCachedTokensFromBody
(
body
[]
byte
)
(
int
,
bool
)
{
if
len
(
body
)
==
0
{
return
0
,
false
}
var
payload
struct
{
Usage
struct
{
CachedTokens
*
int
`json:"cache_n"`
}
`json:"timings"`
}
if
err
:=
common
.
Unmarshal
(
body
,
&
payload
);
err
!=
nil
{
return
0
,
false
}
return
*
payload
.
Usage
.
CachedTokens
,
true
}
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