Skip to main content
The @outputai/http package provides Fetch and Ky clients instrumented for tracing. Every request — URL, method, status code, and timing — is recorded as a child node in the trace tree. It exposes two entry points:
  • outputFetch — a Fetch-compatible function backed by Undici.
  • createKyClient — creates a Ky client that uses outputFetch.
Ky and Undici are peer dependencies. Current npm and pnpm versions install compatible peers automatically, so the normal package installation is enough:
Install Ky or Undici explicitly only when you need to pin their versions or resolve a peer-version conflict.

Using Instrumented Fetch

Use outputFetch when you want the standard Fetch API:
Like native Fetch, outputFetch returns responses for HTTP error statuses. It only throws for failures such as DNS errors, timeouts, and aborts. outputFetch accepts URL strings, URL objects, and both Node and Undici Request objects. Node Request and FormData inputs are normalized into the Undici realm before the request is sent. Keep Request and FormData from the same family when using both in one call.

Creating a Ky Client

The typical pattern is to create a client per external service in your clients directory, then import it in your steps:
clients/jina.ts
createKyClient accepts Ky’s complete options object and returns a standard Ky instance. All Ky methods, hooks, retries, timeout behavior, and .extend() support remain available. By default, the client uses outputFetch. Providing Ky’s fetch option overrides it, so requests made by that client are not automatically traced by Output. You can extend clients to create specialized instances:

HTTP Methods

Response Bodies

Both APIs follow normal Fetch semantics: callers own the returned response body. Always consume the body with .json(), .text(), .arrayBuffer(), or another body reader when you need the payload. If you only need response metadata from a non-HEAD request, such as response.url, response.status, or headers, cancel the unused body so the underlying connection and native buffers can be released:

Using in Steps

Wrap HTTP calls in steps for automatic retry and tracing:
steps.ts

Tracing

All requests made with @outputai/http are automatically traced — no configuration needed. In your trace files, HTTP calls appear as children of the step that made them:
See Tracing for details on the trace format.

Request Events

Every call made through outputFetch or a client returned by createKyClient emits an http:request hook event — independent of whether you attach a cost. Subscribe to it with on for logging, alerting, or per-vendor metrics:
The handler receives an event envelope. Request-specific fields are available under payload: The event fires for every call — success, non-2xx responses, and network failures alike. The existing cost:http:request event is unchanged and continues to fire only when you call addRequestCost.

Attaching Request Cost

When you know the dollar cost of an HTTP request (for example from provider billing headers), you can attach it to the HTTP trace event with addRequestCost.
addRequestCost accepts the total request cost as a number. addRequestCost only works with responses returned by outputFetch or createKyClient. If the response did not come from this package, the function safely no-ops and logs a warning. It also emits a cost:http:request hook event (same hooks system as cost:llm:request). For the payload and examples, see Cost Events — HTTP.

Error Handling

outputFetch returns non-2xx responses without throwing. Ky clients use Ky’s configured error behavior and throw ky.HTTPError for non-2xx responses by default and ky.TimeoutError for timeouts:
Since steps retry automatically, you generally just need to handle cases where retrying won’t help (like a 404). For everything else, let the error propagate and the step’s retry policy will handle it.

Undici Dispatchers

Requests use an EnvHttpProxyAgent by default, including standard HTTP proxy environment variables. You can provide an Undici dispatcher for one request:
Set dispatcher: undefined explicitly to bypass the default dispatcher.

Ky and Undici Exports

The complete Ky and Undici namespaces are re-exported for convenience and to let callers use the same Undici realm as outputFetch:

Environment Variables

API Reference

For complete TypeScript API documentation, see the HTTP Module API Reference.