diff --git a/include/splinter.h b/include/splinter.h index 293d92b..cbf5a23 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -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 */ diff --git a/libsplinter.so b/libsplinter.so index 7b2a067..6461103 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/plugins/cli/cli.so b/plugins/cli/cli.so index 4a7e16b..35dbdaf 100755 Binary files a/plugins/cli/cli.so and b/plugins/cli/cli.so differ diff --git a/src/agent.c b/src/agent.c index f10244b..c82f892 100644 --- a/src/agent.c +++ b/src/agent.c @@ -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 : ""; diff --git a/src/openai.c b/src/openai.c index 477bd0c..536d263 100644 --- a/src/openai.c +++ b/src/openai.c @@ -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); diff --git a/src/sse.c b/src/sse.c index af5593a..f358fea 100644 --- a/src/sse.c +++ b/src/sse.c @@ -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; diff --git a/src/ui.c b/src/ui.c index 1356e29..a0922b8 100644 --- a/src/ui.c +++ b/src/ui.c @@ -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); diff --git a/tests/test_cli b/tests/test_cli index 2a7b3f3..31485cb 100755 Binary files a/tests/test_cli and b/tests/test_cli differ diff --git a/tests/test_compactor b/tests/test_compactor index 2d24258..bdaa546 100755 Binary files a/tests/test_compactor and b/tests/test_compactor differ diff --git a/tests/test_openai b/tests/test_openai index c59cdc6..1b991a3 100755 Binary files a/tests/test_openai and b/tests/test_openai differ diff --git a/tests/test_plugin b/tests/test_plugin index 49fabe2..80d94c5 100755 Binary files a/tests/test_plugin and b/tests/test_plugin differ diff --git a/tests/test_sse b/tests/test_sse index 91203bc..1cbb738 100755 Binary files a/tests/test_sse and b/tests/test_sse differ