@agent-feedback/node package provides drop-in middleware for Express and Fastify. Add it once and every eligible JSON object response gains an _agentFeedback envelope, HTML responses receive an injected <script> tag, and all other responses fall back to the Agent-Feedback header. No handler changes are required, and the feedback telemetry queue never blocks your product response.
Requirements
Node.js 20 or later is required. Express 4–5 and Fastify 4–5 are each optional peer dependencies — install only the one you use.Installation
1
Install the package
2
Create a product key
Open the Agent Feedback dashboard, create a product, and copy the API key. Store it as
AGENT_FEEDBACK_KEY in your environment.Express middleware
ImportagentFeedback from the @agent-feedback/node/express entry point and register it as a global middleware before your routes.
Named operation override
By default the middleware derives the operation name from the Express route path. Usemiddleware.wrap to assign an explicit name for any individual handler — useful when one route serves multiple logical operations.
Graceful shutdown
Callmiddleware.shutdown() before your process exits so the telemetry queue drains cleanly.
Fastify plugin
ImportagentFeedback from @agent-feedback/node/fastify and register it with fastify.register. The plugin uses fastify-plugin so it shares the same Fastify scope as your routes.
Graceful shutdown
The plugin automatically hooks into Fastify’sonClose lifecycle. If you need to shut down the telemetry queue independently, call plugin.shutdown() directly.
Configuration options
Pass these options to eitheragentFeedback constructor. All fields except apiKey are optional.
string
required
Your Agent Feedback product key. Must match the pattern
af_live_<keyId>_<secret>. Store this in an environment variable — never commit it to source control.string
Override the Agent Feedback API endpoint. Useful for testing against a staging environment. Falls back to the
AGENT_FEEDBACK_URL environment variable if set.string[]
Glob-style path patterns that opt routes in. When this array is empty, all routes are instrumented (subject to
exclude). Supports * (any segment) and ** (any path). Examples: ["/search", "/docs/*"].string[]
Glob-style path patterns to skip. Health probes, metrics, and internal endpoints are excluded by default (
/health, /healthz, /metrics, /favicon.ico, /robots.txt, /_agent-feedback/*, /api/v2/outcomes). Patterns you add here extend the default list."auto" | "ask" | "off"
default:"\"auto\""
Controls the feedback instruction sent to agents.
"auto"— instructs the agent to submit a review autonomously without asking the user."ask"— instructs the agent to submit only when the outcome is unambiguous."off"— disables instrumentation entirely.
(request: Request) => string | undefined | null
Callback that extracts an opaque customer identifier from the request. Agent Feedback never uses this to identify individual end-users — it groups interactions by account for aggregate insights.
(request: Request) => string | undefined | null
Callback that extracts a session identifier from the request. Sessions allow Agent Feedback to link a sequence of interactions in the dashboard.
(request: Request) => string | undefined | null
Callback that returns a hint about the agent runtime making the request (e.g.
"claude", "gpt-4o"). Used for runtime-level analytics.Pick<Console, 'debug' | 'warn'>
default:"console"
Provide a custom logger. The SDK calls
.warn() for non-fatal delivery failures and .debug() for internal diagnostics.number
default:"500"
How often the telemetry queue flushes in milliseconds. The queue also flushes immediately when it reaches 50 events.
number
default:"1000"
Maximum number of telemetry events held in memory. When the queue is full, the oldest event is dropped to make room for the newest.
Agent helper functions
The@agent-feedback/node/agent entry point (also re-exported from the main entry) provides two functions for building feedback-aware agent runtimes that submit outcomes deterministically rather than relying on the agent to notice the response envelope.
feedbackFromResponse
Reads the FeedbackEnvelope from a response without executing it. Checks the parsed body first (JSON _agentFeedback field or HTML <script id="agent-feedback"> tag), then falls back to the Agent-Feedback response header.
submitProductOutcome
Submits a compact outcome review to the URL embedded in the envelope. The helper validates the envelope, enforces the allowed-origin allow-list, and throws on invalid input or HTTP errors.
submitProductOutcome refuses to POST to any origin not in allowedSubmitOrigins. The hosted Agent Feedback service is trusted by default. Pass additional origins only when running a self-hosted endpoint.