splinter: exact token counts from the provider's usage block
The sidebar counted characters and guessed tokens. The provider reports the real numbers in the final chunk (prompt_tokens/completion_tokens, or input_tokens/output_tokens), so both the streaming accumulator and the one-shot reply parser now keep them, the agent sums them per turn, and the sidebar shows '79 tok (in 344)' instead of an estimate. The estimate stays as the fallback for endpoints that report nothing, and it is still labelled as one.
This commit is contained in:
1 parent
4ce25255a7
commit
95a57fdbbc
12 files changed
+74
-2
No files matched your search
@@ -112,6 +112,8 @@ struct sp_toolcall {
|
||||
|
||||
struct sp_reply {
|
||||
long status;
|
||||
long tokens_in; /* provider-reported, 0 when unknown */
|
||||
long tokens_out;
|
||||
char *content; /* assistant text, may be "" */
|
||||
struct sp_toolcall *calls;
|
||||
char *calls_json; /* the provider's own tool_calls array */
|
||||
@@ -203,6 +205,8 @@ struct sp_agent_config {
|
||||
|
||||
struct sp_agent_stats {
|
||||
size_t steps;
|
||||
size_t tokens_in;
|
||||
size_t tokens_out;
|
||||
size_t compactions;
|
||||
size_t tool_calls;
|
||||
size_t denied;
|
||||
@@ -249,6 +253,7 @@ int sp_sse_line(struct sp_sse *, const char *line, struct sp_buf *out,
|
||||
/* Turn the accumulated state into a reply. Returns 0 or -1. */
|
||||
int sp_sse_finish(struct sp_sse *, struct sp_reply *out, struct sp_buf *err);
|
||||
int sp_sse_done(const struct sp_sse *);
|
||||
int sp_sse_usage(const struct sp_sse *, long *in, long *out);
|
||||
const char *sp_sse_tool_start(const struct sp_sse *, size_t index);
|
||||
|
||||
#endif /* SPLINTER_H */
|
||||
Binary file not shown.
Binary file not shown.
@@ -163,6 +163,11 @@ sp_agent_run(struct sp_agent *a, const char *user_text, struct sp_buf *answer,
|
||||
goto out;
|
||||
}
|
||||
|
||||
a->stats.tokens_in += (size_t)(reply.tokens_in > 0 ?
|
||||
reply.tokens_in : 0);
|
||||
a->stats.tokens_out += (size_t)(reply.tokens_out > 0 ?
|
||||
reply.tokens_out : 0);
|
||||
|
||||
if (reply.ncalls == 0) {
|
||||
const char *text = reply.content != NULL ?
|
||||
reply.content : "";
|
||||
|
||||
@@ -208,6 +208,25 @@ sp_openai_reply(const char *json, struct sp_reply *out, struct sp_buf *err)
|
||||
return -1;
|
||||
}
|
||||
|
||||
{
|
||||
const mcp_json *usage = mcp_arg(v, "usage");
|
||||
|
||||
if (usage != NULL) {
|
||||
long pin = (long)mcp_arg_int(usage, "prompt_tokens", -1);
|
||||
long pout = (long)mcp_arg_int(usage, "completion_tokens",
|
||||
-1);
|
||||
|
||||
if (pin < 0)
|
||||
pin = (long)mcp_arg_int(usage, "input_tokens", -1);
|
||||
if (pout < 0)
|
||||
pout = (long)mcp_arg_int(usage, "output_tokens", -1);
|
||||
if (pin >= 0)
|
||||
out->tokens_in = pin;
|
||||
if (pout >= 0)
|
||||
out->tokens_out = pout;
|
||||
}
|
||||
}
|
||||
|
||||
calls = mcp_arg(message, "tool_calls");
|
||||
if (calls != NULL && mcp_array_len(calls) > 0) {
|
||||
size_t i, n = mcp_array_len(calls);
|
||||
|
||||
@@ -35,6 +35,8 @@ struct sp_sse {
|
||||
struct sse_call calls[MAX_CALLS];
|
||||
size_t ncalls;
|
||||
int done;
|
||||
long tokens_in;
|
||||
long tokens_out;
|
||||
};
|
||||
|
||||
struct sp_sse *
|
||||
@@ -68,6 +70,18 @@ sp_sse_free(struct sp_sse *s)
|
||||
free(s);
|
||||
}
|
||||
|
||||
int
|
||||
sp_sse_usage(const struct sp_sse *s, long *in, long *out)
|
||||
{
|
||||
if (s == NULL)
|
||||
return 0;
|
||||
if (in != NULL)
|
||||
*in = s->tokens_in;
|
||||
if (out != NULL)
|
||||
*out = s->tokens_out;
|
||||
return (s->tokens_in > 0 || s->tokens_out > 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_sse_done(const struct sp_sse *s)
|
||||
{
|
||||
@@ -133,6 +147,25 @@ sp_sse_line(struct sp_sse *s, const char *line, struct sp_buf *out,
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* llama.cpp and OpenAI both attach usage to the last chunk */
|
||||
{
|
||||
const mcp_json *usage = mcp_arg(v, "usage");
|
||||
|
||||
if (usage != NULL) {
|
||||
long in = (long)mcp_arg_int(usage, "prompt_tokens", -1);
|
||||
long out = (long)mcp_arg_int(usage, "completion_tokens", -1);
|
||||
|
||||
if (in < 0)
|
||||
in = (long)mcp_arg_int(usage, "input_tokens", -1);
|
||||
if (out < 0)
|
||||
out = (long)mcp_arg_int(usage, "output_tokens", -1);
|
||||
if (in >= 0)
|
||||
s->tokens_in = in;
|
||||
if (out >= 0)
|
||||
s->tokens_out = out;
|
||||
}
|
||||
}
|
||||
|
||||
choices = mcp_arg(v, "choices");
|
||||
if (choices == NULL || mcp_array_len(choices) == 0) {
|
||||
/* keepalive and usage-only events land here */
|
||||
@@ -239,6 +272,8 @@ sp_sse_finish(struct sp_sse *s, struct sp_reply *out, struct sp_buf *err)
|
||||
if (s == NULL || out == NULL)
|
||||
return -1;
|
||||
out->status = 200;
|
||||
out->tokens_in = s->tokens_in;
|
||||
out->tokens_out = s->tokens_out;
|
||||
out->content = strdup(s->content.p != NULL ? s->content.p : "");
|
||||
if (out->content == NULL)
|
||||
return -1;
|
||||
|
||||
@@ -46,6 +46,8 @@ struct ui_state {
|
||||
size_t n;
|
||||
size_t scroll; /* lines scrolled back from the bottom */
|
||||
long last_ms;
|
||||
size_t tokens_in;
|
||||
size_t tokens_out;
|
||||
size_t out_chars; /* characters streamed in this/last turn */
|
||||
size_t think_back; /* lines scrolled back in the thinking field */
|
||||
struct sp_buf last_think; /* the previous turn's reasoning */
|
||||
@@ -451,8 +453,12 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir,
|
||||
y++;
|
||||
|
||||
sidebar_heading(&y, cols, "output");
|
||||
mvwprintw(ui_side, y++, 0, "%zu chars (~%zu tok)",
|
||||
s->out_chars, s->out_chars / 4);
|
||||
if (s->tokens_out > 0)
|
||||
mvwprintw(ui_side, y++, 0, "%zu tok (in %zu)",
|
||||
s->tokens_out, s->tokens_in);
|
||||
else
|
||||
mvwprintw(ui_side, y++, 0, "%zu chars (~%zu tok)",
|
||||
s->out_chars, s->out_chars / 4);
|
||||
y++;
|
||||
|
||||
sidebar_heading(&y, cols, "widgets");
|
||||
@@ -854,6 +860,8 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
|
||||
(t1.tv_nsec - t0.tv_nsec) / 1000000);
|
||||
s.last_steps = ag->stats.steps;
|
||||
s.last_calls = ag->stats.tool_calls;
|
||||
s.tokens_in = ag->stats.tokens_in;
|
||||
s.tokens_out = ag->stats.tokens_out;
|
||||
|
||||
if (rc == 0)
|
||||
add_entry(&s, "agent", answer.p);
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user