api

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Version = "0.1.0"

Variables

This section is empty.

Functions

func Middleware

func Middleware(t *Tracker) func(http.Handler) http.Handler

Middleware records each API call the handler serves.

func RouteFromPath

func RouteFromPath(path string) string

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.

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

Tracker buffers agents' calls and per-day counts of the rest.

func New

func New(o Options) *Tracker

New returns a Tracker; without an API key it does nothing.

func (*Tracker) Flush

func (t *Tracker) Flush(ctx context.Context) error

Flush sends what's buffered now. Failed batches are dropped, never retried inline.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL