ledgerv1
import ledgerv1 "pocketone.dev/ext/contracts/llm/ledger/v1"
Package ledgerv1 is the llm.ledger/v1 contract: what an app's model calls have used and cost, read from the ledger every llm.* call leaves (the provider's own record of each call), and how much of each limit is left.
A wire contract at /api/_c/llm.ledger/v1. In pb_hooks it's $llm.ledger.
Source: ext/contracts/llm/ledger/v1.
Variables
var Contract = extkit.NewContract[Ledger]("llm.ledger/v1", extkit.One).JS(extkit.JSHooks, nil).JSTypes(jsTypes)
Contract is llm.ledger/v1.
var W = extkit.NewWire(Contract, "llm:read", func(inner Ledger, g *extkit.Guard[Ledger]) Ledger {
return &guardLedger{inner, g}
},
extkit.Op[Ledger]{
Name: "remaining",
Desc: "Is, for each limit in Settings → LLM that counts the subject's calls, how much is used and left in its current window, the tightest first.",
Method: "GET",
Path: "remaining",
Scope: "public",
In: reflect.TypeOf(RemainingInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Ledger, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in RemainingInput
if err := extkit.DecodeQuery(req.Query, &in); err != nil {
return nil, err
}
return impl.Remaining(ctx, a, in)
},
},
extkit.Op[Ledger]{
Name: "spent",
Desc: "Is what the subject's calls used in a window.",
Method: "GET",
Path: "spent",
Scope: "public",
In: reflect.TypeOf(SpentInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Ledger, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in SpentInput
if err := extkit.DecodeQuery(req.Query, &in); err != nil {
return nil, err
}
return impl.Spent(ctx, a, in)
},
},
extkit.Op[Ledger]{
Name: "summary",
Desc: "Is what calls used, grouped by model, kind, subject or day.",
Method: "GET",
Path: "summary",
In: reflect.TypeOf(SummaryInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Ledger, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in SummaryInput
if err := extkit.DecodeQuery(req.Query, &in); err != nil {
return nil, err
}
return impl.Summary(ctx, a, in)
},
},
extkit.Op[Ledger]{
Name: "calls",
Desc: "Lists the ledger's rows, newest first.",
Method: "GET",
Path: "calls",
In: reflect.TypeOf(CallsInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Ledger, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in CallsInput
if err := extkit.DecodeQuery(req.Query, &in); err != nil {
return nil, err
}
return impl.Calls(ctx, a, in)
},
},
)
W is llm.ledger/v1's wire side: routes at /api/_c/llm.ledger/v1/<op>, action hooks, MCP tools and the client.
Functions
OnCalls
func OnCalls(owner any, fn func(e *extkit.CallEvent, in *CallsInput) error)
OnCalls binds fn to calls calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnRemaining
func OnRemaining(owner any, fn func(e *extkit.CallEvent, in *RemainingInput) error)
OnRemaining binds fn to remaining calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnSpent
func OnSpent(owner any, fn func(e *extkit.CallEvent, in *SpentInput) error)
OnSpent binds fn to spent calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnSummary
func OnSummary(owner any, fn func(e *extkit.CallEvent, in *SummaryInput) error)
OnSummary binds fn to summary calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
Types
Call
type Call struct {
ID string `json:"id"`
Created string `json:"created"`
Kind string `json:"kind"`
Model string `json:"model"`
Subject string `json:"subject"`
Via string `json:"via"`
Input int64 `json:"input"`
Cached int64 `json:"cached"`
Output int64 `json:"output"`
Seconds float64 `json:"seconds"`
CostMicroUSD int64 `json:"costMicroUSD"`
Estimated bool `json:"estimated"`
Refused string `json:"refused,omitempty"` // why a limit refused it
Job string `json:"job,omitempty"`
}
Call is one row of the ledger.
CallList
type CallList struct {
Calls []Call `json:"calls"`
More bool `json:"more"` // there are more after this page
}
CallList is a page of rows.
CallsInput
type CallsInput struct {
// Window is "day" or "month": the UTC calendar day or month holding now. Or give Since and Until (RFC 3339 or
// YYYY-MM-DD, UTC; Until is exclusive). With none of them, all of the ledger (it keeps 13 months).
Window string `json:"window,omitempty"`
Since string `json:"since,omitempty"`
Until string `json:"until,omitempty"`
Subject string `json:"subject,omitempty"`
Kind string `json:"kind,omitempty"`
Model string `json:"model,omitempty"`
// Status is "ok" for calls that reached a provider, "refused" for calls a limit refused, empty for both.
Status string `json:"status,omitempty"`
Limit int `json:"limit,omitempty"` // default 50, at most 500
Offset int `json:"offset,omitempty"`
}
CallsInput filters and pages the rows.
Client
type Client struct {
// contains filtered or unexported fields
}
Client is llm.ledger/v1 over HTTP, for the CLI and other apps. It implements Ledger; the token it was made with is the credential, so the extkit.Auth argument is ignored.
NewClient
func NewClient(baseURL, token string, hc *http.Client) *Client
NewClient calls llm.ledger/v1 on the app at baseURL with token (a PocketBase auth token or an API token).
Client.Calls
func (c *Client) Calls(ctx context.Context, _ extkit.Auth, in CallsInput) (CallList, error)
Client.Remaining
func (c *Client) Remaining(ctx context.Context, _ extkit.Auth, in RemainingInput) (Remaining, error)
Client.Spent
func (c *Client) Spent(ctx context.Context, _ extkit.Auth, in SpentInput) (Totals, error)
Client.Summary
func (c *Client) Summary(ctx context.Context, _ extkit.Auth, in SummaryInput) (Summary, error)
Ledger
type Ledger interface {
// Remaining is, for each limit in Settings → LLM that counts the subject's calls, how much is used and left in
// its current window, the tightest first.
//
//op: GET remaining scope=public
Remaining(ctx context.Context, a extkit.Auth, in RemainingInput) (Remaining, error)
// Spent is what the subject's calls used in a window.
//
//op: GET spent scope=public
Spent(ctx context.Context, a extkit.Auth, in SpentInput) (Totals, error)
// Summary is what calls used, grouped by model, kind, subject or day.
//
//op: GET summary
Summary(ctx context.Context, a extkit.Auth, in SummaryInput) (Summary, error)
// Calls lists the ledger's rows, newest first.
//
//op: GET calls
Calls(ctx context.Context, a extkit.Auth, in CallsInput) (CallList, error)
}
Ledger reads the ledger.
Remaining and Spent answer about one subject (whom calls were for: see llm.WithSubject). A superuser, an API token with the llm:read scope, and page code may name any subject; a signed-in user is answered about themselves only. Page code calling through $llm.for(e) gets the request's own subject when it names none.
LimitLeft
type LimitLeft struct {
// Limit is the limit in words: "each guest, $0.10 a day".
Limit string `json:"limit"`
Scope string `json:"scope"` // app, user, guest or guests
Window string `json:"window"` // day or month
Metric string `json:"metric"` // cost, input, output or calls
Kind string `json:"kind,omitempty"`
Model string `json:"model,omitempty"`
// Amount, Used and Left are in the metric's unit: US dollars for cost, else tokens or calls.
Amount float64 `json:"amount"`
Used float64 `json:"used"`
Left float64 `json:"left"`
// Resets is when the window ends (RFC 3339, UTC).
Resets string `json:"resets"`
}
LimitLeft is one limit and how much of it is left.
Remaining
type Remaining struct {
Limits []LimitLeft `json:"limits"`
}
Remaining is the limits that count a subject's calls.
RemainingInput
type RemainingInput struct {
// Subject is whom to answer about ("user:users/abc", "guest:<id>", "team:acme"); empty means the caller's own
// subject, or none, which only limits on the whole app count.
Subject string `json:"subject,omitempty"`
}
RemainingInput names the subject.
SpentInput
type SpentInput struct {
// Subject: as for Remaining, except that empty with no caller's subject means every call (the whole app).
Subject string `json:"subject,omitempty"`
// Window is "day" or "month": the UTC calendar day or month holding now. Or give Since and Until (RFC 3339 or
// YYYY-MM-DD, UTC; Until is exclusive). With none of them, all of the ledger (it keeps 13 months).
Window string `json:"window,omitempty"`
Since string `json:"since,omitempty"`
Until string `json:"until,omitempty"`
Kind string `json:"kind,omitempty"`
Model string `json:"model,omitempty"`
}
SpentInput is a subject and a window, optionally narrowed to a kind or a model.
Summary
type Summary struct {
Rows []SummaryRow `json:"rows"`
}
Summary is the groups.
SummaryInput
type SummaryInput struct {
// By is what to group by: "model" (the default), "kind", "subject" or "day".
By string `json:"by,omitempty"`
// Window is "day" or "month": the UTC calendar day or month holding now. Or give Since and Until (RFC 3339 or
// YYYY-MM-DD, UTC; Until is exclusive). With none of them, all of the ledger (it keeps 13 months).
Window string `json:"window,omitempty"`
Since string `json:"since,omitempty"`
Until string `json:"until,omitempty"`
Subject string `json:"subject,omitempty"`
Kind string `json:"kind,omitempty"`
Model string `json:"model,omitempty"`
// Limit is how many groups, the costliest first (by day: the latest first); default 100, at most 1000.
Limit int `json:"limit,omitempty"`
}
SummaryInput groups calls in a window.
SummaryRow
type SummaryRow struct {
Key string `json:"key"` // the model, kind, subject or day (YYYY-MM-DD)
Totals
}
SummaryRow is one group's totals.
Totals
type Totals struct {
Calls int64 `json:"calls"` // calls that reached a provider
Refused int64 `json:"refused"` // calls a limit refused
Input int64 `json:"input"`
Cached int64 `json:"cached"`
Output int64 `json:"output"`
Seconds float64 `json:"seconds"`
CostMicroUSD int64 `json:"costMicroUSD"` // millionths of a US dollar
}
Totals is what calls used.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.