Documentation
¶
Overview ¶
Package api tracks which routes AI agents call on your API, and counts every other call.
t := api.New(api.Options{}) // AGENTSTATS_API_KEY (al_api_…)
http.ListenAndServe(":8080", api.Middleware(t)(mux))
A call counts as an agent's when it carries a Web Bot Auth signature, an X-AI-Agent header (cli.AgentHeaders, set while a coding agent runs the client), an AI agent's User-Agent, or a credential you name in Options.Caller. AgentStats checks signatures and User-Agents again before keeping a call. What leaves your server for an agent's call: method, route pattern, status, duration, the User-Agent and X-AI-Agent headers, and the caller as an HMAC of its credential keyed with your API key. For every other call: a count per day. Never bodies, query strings, cookies or credentials. Recording is a timestamp and an append under a mutex; a goroutine sends.
Index ¶
Constants ¶
const Version = "0.1.0"
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
Middleware records each API call the handler serves.
func RouteFromPath ¶
RouteFromPath is a path as a route pattern: values (ids, UUIDs, tokens, emails) become :id.
Types ¶
type Call ¶
type Call struct {
Ts int64 `json:"ts"`
Method string `json:"method"`
Route string `json:"route"`
Status int `json:"status"`
DurationMs float64 `json:"durationMs"`
UA *string `json:"ua"`
IP *string `json:"ip"`
URL *string `json:"url"`
Headers map[string]string `json:"headers"`
Agent *string `json:"agent"`
Run *string `json:"run"`
Actor *string `json:"actor"`
ActorAgent *string `json:"actorAgent"`
}
Call is one agent's call as sent to AgentStats.
type Options ¶
type Options struct {
APIKey string // AGENTSTATS_API_KEY
// Routes says which requests are API calls. Default: all of them, or with ServesPages only
// paths under /api and /v1, /v2…
Routes func(path string) bool
ServesPages bool
// Caller identifies who made a call, so one agent's calls group into a session: an API key id,
// an OAuth client and user. It's sent as an HMAC, never as is. Return agent to name the agent
// behind a credential you know. Default: the Authorization, X-API-Key or API-Key header.
Caller func(r *http.Request) (id, agent string)
// NoCaller turns grouping by caller off.
NoCaller bool
// RouteOf returns the matched route pattern for routers other than net/http's ServeMux (whose
// r.Pattern is read by default), e.g. chi.RouteContext(r.Context()).RoutePattern().
RouteOf func(r *http.Request) string
FlushInterval time.Duration // default 5s
MaxBatch int // default 100
MaxBuffer int // default 10000
Debug bool // AGENTSTATS_DEBUG=1
HTTPClient *http.Client
// contains filtered or unexported fields
}
Options configure a Tracker. Zero values fall back to environment variables and defaults.