Users and guests
Limits on each user, each guest and all guests together need to know which user or guest made each call.
Page code says so with one change: call the model through $llm.for(ctx) instead of $llm. In Go, the same thing is
the call's context, carrying the subject llm.SubjectOf(app, e) works out from the request.
// pb_pages/ask.ts
export async function post(ctx: pages.Context) {
const { question } = ctx.req.body()
const r: LlmTextAnswer = await $llm.for(ctx).text.completeAsync({ model: "vultr/glm-5.3", prompt: question })
return ctx.res.json({ answer: r.text })
}
se.Router.POST("/ask", func(e *core.RequestEvent) error {
body := struct{ Question string }{}
if err := e.BindBody(&body); err != nil {
return e.BadRequestError("send {question}", err)
}
ctx := llmc.WithSubject(e.Request.Context(), llm.SubjectOf(app, e))
r, err := textv1.Contract.Must(app).Complete(ctx, extkit.Internal, textv1.CompleteInput{
Model: "vultr/glm-5.3", Prompt: body.Question})
if err != nil {
return err
}
return e.JSON(200, map[string]string{"answer": r.Text})
})
$llm.for() takes the request you're answering: a page's ctx, or a pb_hooks route's e. It returns the same API as
$llm, every kind, chats and streams included, with each call counted for that request:
- its signed-in user, when the request carries an auth record (counted as
user:<collection>/<id>); - else a guest, told apart by a cookie (below).
Keep the object (in Go, the context) for the whole request when you make several calls, so they all count for the same person:
const llm = $llm.for(ctx)
const chat = llm.chat.open(chatId, { model: "vultr/glm-5.3" })
const r = await chat.sendAsync(question)
const more = await llm.text.completeAsync({ model: "vultr/glm-5.3", prompt: `Three follow-up questions to: ${question}` })
ctx := llmc.WithSubject(e.Request.Context(), llm.SubjectOf(app, e))
r, err := chatv1.Contract.Must(app).Send(ctx, extkit.Internal, chatv1.SendInput{ID: chatID, Text: question, Model: "vultr/glm-5.3"})
// ...
more, err := textv1.Contract.Must(app).Complete(ctx, extkit.Internal, textv1.CompleteInput{
Model: "vultr/glm-5.3", Prompt: "Three follow-up questions to: " + question})
Calls that aren't for anyone
Plain $llm, or a context with no subject, counts toward limits on the whole app only. Use it for work that isn't
on someone's behalf: a cron, a hook that tags new records, a migration. Those calls are recorded with no subject, never
count as a guest's, and are never refused for a missing guest total.
// pb_hooks/tags.pb.ts: tags every new post; counted toward the whole app only
onRecordAfterCreateSuccess(async (e) => {
const r = await $llm.text.completeAsync({ model: "vultr/glm-5.3", prompt: "Three tags for: " + e.record.get("title") })
e.record.set("tags", r.text)
$app.save(e.record)
e.next()
}, "posts")
app.OnRecordAfterCreateSuccess("posts").BindFunc(func(e *core.RecordEvent) error {
r, err := textv1.Contract.Must(e.App).Complete(context.Background(), extkit.Internal, textv1.CompleteInput{
Model: "vultr/glm-5.3", Prompt: "Three tags for: " + e.Record.GetString("title")})
if err == nil {
e.Record.Set("tags", r.Text)
err = e.App.Save(e.Record)
}
if err != nil {
return err
}
return e.Next()
})
Calls over HTTP and MCP (/api/_c/llm.text/v1/...) come from superusers and API tokens, never from visitors, so they
also count toward the whole app only.
Guests
A visitor who isn't signed in is a guest. Guests are told apart by a cookie, pocketone_llm_guest, which the first
$llm.for() call (in Go, llm.SubjectOf) for a visitor sets on the response:
- It holds a random id and a signature, so a visitor can't pick someone else's id. The signing key is made the first
time it's needed and kept in the app's key-value store at the private name
_llm/guest-key. Deleting it gives every guest a new id. - It lasts a year, is
HttpOnly(page JavaScript can't read it),SameSite=Lax, andSecurewhen the app is served over HTTPS. - Until the browser sends it back, the visitor counts by IP address (
guest:ip:<address>). The first call of a visit is counted that way, and so is every call from a client that never keeps cookies, so dropping cookies doesn't get a fresh allowance on every call.
A visitor who clears their cookies comes back as a new guest. That's why guest calls need a limit on all guests together before they're answered: it's the only limit a visitor can't reset.
When a call is refused
A refused call fails, whether a built-in limit refused it or one of your onCall handlers did. Its
code is limit and its message is written for the person who made the request ("limit reached: each guest, $0.10 a day
(used $0.10); it resets at 00:00 UTC", or your handler's own words), so the simplest handling is to show it:
// pb_pages/ask.ts
export async function post(ctx: pages.Context) {
const { question } = ctx.req.body()
try {
const r: LlmTextAnswer = await $llm.for(ctx).text.completeAsync({ model: "vultr/glm-5.3", prompt: question })
return ctx.res.json({ answer: r.text })
} catch (err: any) {
if (err.code === "limit") return ctx.res.json(429, { error: err.message })
throw err
}
}
r, err := textv1.Contract.Must(app).Complete(ctx, extkit.Internal, textv1.CompleteInput{Model: "vultr/glm-5.3", Prompt: question})
var xe *extkit.Error
if errors.As(err, &xe) && xe.Code == llmc.ErrLimit.Code {
return e.JSON(429, map[string]string{"error": xe.Message})
}
if err != nil {
return err
}
return e.JSON(200, map[string]string{"answer": r.Text})
A streamed answer fails the same way, before any text arrives. With server-sent events, send the message as an event the page shows in place of the answer, as pocketone.dev's ask box does:
// pb_pages/chat.ts
export async function post(ctx: pages.Context) {
const { question } = ctx.req.body()
const stream = ctx.res.sse()
try {
const r = await $llm.for(ctx).text.streamAsync({ model: "vultr/glm-5.3", prompt: question },
(text) => stream.send("text", JSON.stringify(text)))
stream.send("done", JSON.stringify({ usage: r.usage }))
} catch (err: any) {
stream.send("error", JSON.stringify(err.message))
}
stream.close()
}
s, err := textv1.Contract.Must(app).Stream(ctx, extkit.Internal, textv1.CompleteInput{Model: "vultr/glm-5.3", Prompt: question})
if err != nil {
return err // a bad model, before the stream starts
}
e.Response.Header().Set("Content-Type", "text/event-stream")
send := func(event string, data any) {
b, _ := json.Marshal(data)
fmt.Fprintf(e.Response, "event: %s\ndata: %s\n\n", event, b)
_ = e.Flush()
}
err = s.Run(ctx, func(v any) error {
if c, ok := v.(textv1.Chunk); ok && c.Text != "" {
send("text", c.Text)
}
return nil
})
if err != nil {
send("error", err.Error()) // a limit's refusal arrives here
}
return nil
Over HTTP, refused calls answer 429 with {"code": "limit", "message": "..."}.
Showing what's left
$llm.for(ctx).ledger.remaining() lists the limits that count this visitor, with what's used and left, so a page can
say "4 questions left today" before anyone hits a refusal (Spend and the ledger):
<% const left = $llm.for(ctx).ledger.remaining()[0] %>
<% if (left) { %><p><%= left.left %> of <%= left.amount %> left today</p><% } %>
ctx := llmc.WithSubject(e.Request.Context(), llm.SubjectOf(app, e))
rem, err := ledgerv1.Contract.Must(app).Remaining(ctx, extkit.Internal, ledgerv1.RemainingInput{})
if err == nil && len(rem.Limits) > 0 {
left := rem.Limits[0] // the tightest
fmt.Fprintf(e.Response, "<p>%v of %v left today</p>", left.Left, left.Amount)
}
The Go side
llmc.WithSubject(ctx, s)andllmc.SubjectOf(ctx)(inpocketone.dev/ext/contracts/llm) set and read the subject;llmc.UserSubject(collection, id)builds a user's. A call whose context has no subject counts toward the whole app only.llm.SubjectOf(app, e)(inpocketone.dev/ext/llm) is what$llm.for(e)uses: the request's user, else its guest cookie (set one's response when missing), else its IP.- Pass the context on and the subject goes with it: LLM Chat's calls to
llm.text/v1count for the subject of the send that made them. - A refused call returns an
*extkit.ErrorwithCode"limit"andStatus429(llmc.ErrLimit's).
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.