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
Unverified
Commit
4d675b4d
authored
Mar 22, 2026
by
Seefs
Committed by
GitHub
Mar 22, 2026
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3357 from wenyifancc/cache_llama_cpp
feat: Add support for counting cache-hit tokens in llama.cpp
parents
87b426f3
2c3ae32c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
0 deletions
+28
-0
relay/channel/openai/relay-openai.go
+28
-0
No files found.
relay/channel/openai/relay-openai.go
View file @
4d675b4d
...
...
@@ -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,25 @@ 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
{
Timings
struct
{
CachedTokens
*
int
`json:"cache_n"`
}
`json:"timings"`
}
if
err
:=
common
.
Unmarshal
(
body
,
&
payload
);
err
!=
nil
{
return
0
,
false
}
if
payload
.
Timings
.
CachedTokens
==
nil
{
return
0
,
false
}
return
*
payload
.
Timings
.
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