A Claude Code turn relayed on Messages with its advisor on has the advisor run at the vendor. The reply's top-level usage is the executor's alone; each advisor call is an advisor_message entry of usage.iterations, billed at the advisor model's rates (Anthropic's advisor tool page, "Usage and billing"). magpie read only the top level, so an Opus advisor beside a Sonnet executor was in no row and priced nowhere. aUsage reads iterations, whole and from a stream's message_delta (whose list is cumulative, so the latest stands for the reply). Each advisor call becomes its own row (advisorRows, written by appendAdvised at every row of a try that reached a provider, and on the native route): under the advisor's model by magpie's id (advisorKeyModel of the request's advisor tool), Served as the vendor names it, Kind "advisor" (named Advisor in Usage's filters, already translated), the request's route, agent, account and session, status 200, counted as its own request as magpie's own advisor calls already are. The executor's row is unchanged. Routing prices the calls too, before the request's own usage so the prompt is still read from the last entry. Iterations with no advisor entry (Copilot) leave Usage as it was. The fixture is Anthropic's documented example word for word; no real reply with an advisor call was captured. The core script budget is raised by 52, the Advisor entry in the generated purposes.js (the base had no room; 1 byte less is red). The kinds are read wherever Usage and Routing draw a row, so there is no page to move it to. Run at this commit's tree onf1b0663f1, then onb1f044fc1(which changed core scripts only, i18n.js and syncfiles.js, so internal/gui and the GUI files were run again there): - mica: gofmt -l (nothing), go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows go vet for internal/gateway, internal/usage, internal/gui: ok. - mica: go test -timeout 45m -count=1 ./internal/gateway ./internal/usage ./internal/gui: ok (916s, 64s, 161s). - mote: node --test purpose-filter, routing-purpose-state, usage-big-write, usage-ledger-mcp, usage-ledger-search, i18n-keys (Chromium and WebKit): 74 of 74 pass. - Onb1f044fc1, the pushed base: mica go test -timeout 45m -count=1 ./internal/gui ok (147s); mote the same six GUI files, 74 of 74 pass. - Before the rebase, atae94571d7, the same packages on mote were ok except TestCoreScriptBudget, which this commit's budget line answers. Broken by hand: - aUsage.usage never setting advised: TestAdvisorIterationsRead red ("stream=false: advisor calls <nil>, want one"), TestAdvisorCallHasItsOwnRow red in both subtests ("advisor rows: []"). - passthrough without advisedAs: TestAdvisorCallHasItsOwnRow red in both subtests, the row named claude-opus-5-5 and not magpie's claude-opus-5.5. Not tried: a real Anthropic reply with an advisor call (no account with the advisor beta at hand); Bedrock and Vertex passthrough with an advisor (they share the sniffer, not run); the Usage page with an advisor row in a real browser session beyond the GUI files above. Reported by Refeccd on Discord.
66 lines
2.8 KiB
Go
66 lines
2.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"cmp"
|
|
"net/http"
|
|
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// advisorRows are the usage rows of the advisor calls a reply says its
|
|
// vendor made (Anthropic's advisor tool, run at the vendor on a request
|
|
// that goes as it is: advisorTools), one a call, beside rec, the
|
|
// executor's own row. Anthropic's top-level usage is the executor's alone
|
|
// and bills each advisor_message iteration at the advisor model's rates,
|
|
// so a row of the request's model held only part of what was spent: an
|
|
// Opus advisor's tokens beside a Sonnet executor were in no row at all
|
|
// (#6977, Refeccd on Discord). Each row is under the advisor's model, by
|
|
// magpie's id the request named it by (advisedAs), else the vendor's name
|
|
// for it, its Served the vendor's; nothing is taken from rec. It is its
|
|
// own request, as an advisor call magpie runs itself is (advise): Kind
|
|
// says it is the advisor's, and it has the request's route, agent,
|
|
// account and session. It has no timing of its own (the vendor's stream
|
|
// tells none), and its status is the call's, which the reply listed as
|
|
// done, not the executor's.
|
|
func advisorRows(rec usage.Record, u Usage) []usage.Record {
|
|
a := u.advised
|
|
if a == nil {
|
|
return nil
|
|
}
|
|
out := make([]usage.Record, 0, len(a.calls))
|
|
for _, c := range a.calls {
|
|
b := c.use
|
|
if b.Input+b.Output+b.CacheRead+b.CacheWrite == 0 {
|
|
continue
|
|
}
|
|
model := cmp.Or(a.model, c.served)
|
|
out = append(out, usage.Record{RouteID: rec.RouteID, Time: rec.Time, Agent: rec.Agent, Via: rec.Via, Provider: rec.Provider, Host: rec.Host,
|
|
ProviderKeyID: rec.ProviderKeyID, ProviderKeyName: rec.ProviderKeyName, ProviderAccount: rec.ProviderAccount,
|
|
Model: model, Requested: model, Served: c.served,
|
|
Input: b.Input, Output: b.Output, CacheRead: b.CacheRead, CacheWrite: b.CacheWrite, CacheWrite1h: b.CacheWrite1h, Reasoning: b.Reasoning,
|
|
Status: http.StatusOK, Session: rec.Session, NativeSession: rec.NativeSession, Kind: usage.KindAdvisor, Endpoint: rec.Endpoint})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// appendAdvised writes rec, then the rows of the advisor calls u says its
|
|
// reply made (advisorRows).
|
|
func appendAdvised(r *http.Request, rec usage.Record, u Usage) {
|
|
appendUsage(r, rec)
|
|
for _, a := range advisorRows(rec, u) {
|
|
appendUsage(r, a)
|
|
}
|
|
}
|
|
|
|
// advisorRouteUsage is the Routing page's pricing of the advisor calls u
|
|
// says its reply made, under provider id, before the request's own (which
|
|
// stays last: promptCounted reads the prompt from it).
|
|
func advisorRouteUsage(id string, u Usage) []RouteUsage {
|
|
var out []RouteUsage
|
|
for _, a := range advisorRows(usage.Record{Provider: id}, u) {
|
|
out = append(out, RouteUsage{Provider: id, Model: a.Model, Input: a.Input, Output: a.Output,
|
|
CacheRead: a.CacheRead, CacheWrite: a.CacheWrite, Reasoning: a.Reasoning})
|
|
}
|
|
return out
|
|
}
|