Skip to content
AgentsKit
Packages

@agentskit/net

Retries with Retry-After, cancellable waits and deadlines, bounded body reads, SSE parsing and SSRF address checks over web APIs.

@agentskit/net is the package for the moment your agent talks HTTP: a provider returns 429 with Retry-After, a stream arrives as server-sent events, a tool fetches a URL a user typed, or a response could be larger than you want in memory. It uses web APIs only, so the same code runs on Node, Bun, Deno, browsers and edge runtimes.

#When to reach for it

  • You call an API that rate-limits or fails transiently and need retries that respect Retry-After.
  • You need a timeout per attempt, not one that silently spans every retry.
  • You need a deadline for async work and a signal that forwards caller cancellation.
  • You consume a server-sent events stream.
  • You fetch URLs supplied by users or models and must not reach private networks or cloud metadata.

#Install

npm install @agentskit/net

#Hello world

import { fetchWithRetry, parseSSEResponse, readJson } from '@agentskit/net'

const response = await fetchWithRetry('https://api.example.com/v1/models', undefined, { timeoutMs: 10_000 })
console.log(await readJson(response, { maxBytes: 1_000_000 }))

for await (const event of parseSSEResponse(await fetch('https://api.example.com/v1/stream'))) {
  console.log(event.data)
}

#Cancellable waits and deadlines

retry uses the exported sleep(ms, signal?) by default. Delays must be from 0 through 2,147,483,647 milliseconds. It clears its timer on abort and rejects with the signal's exact reason. To replace the wait, pass sleep: (ms, signal?) => Promise<void>; one-argument callbacks still work, and callbacks should honor the optional signal when their own wait must stop on cancellation.

withTimeout(work, ms, parent?) accepts a positive deadline up to 2,147,483,647 milliseconds and gives work a signal that aborts at the deadline or with the parent's exact reason. The deadline rejects with AK_NET_TIMEOUT; invalid values reject with AK_NET_INVALID_INPUT. It bounds the wrapper's rejection even if work ignores the signal, but only cooperative work such as fetch can be stopped. A success value or original work failure passes through unchanged.

retry retries rejected work, such as fetch network errors; it does not inspect HTTP status codes. Use fetchWithRetry when retrying retryable HTTP responses.

import { retry, sleep, withTimeout } from '@agentskit/net'

const parent = new AbortController()
const response = await withTimeout(
  signal => fetch('https://api.example.com/data', { signal }),
  5_000,
  parent.signal,
)
console.log(response.status)

await retry(
  ({ signal }) => fetch('https://api.example.com/models', { signal }),
  { signal: parent.signal, sleep: (ms, signal) => sleep(ms, signal) },
)

#What it replaces

Hand-rolled patternUse
for loop with a fixed sleepretry(fn) / fetchWithRetry(url)
Ignoring or re-parsing Retry-AfterparseRetryAfter(header) (built into fetchWithRetry)
Promise.race with setTimeouttimeoutSignal(ms), anySignal([...]), withTimeout(work, ms)
await res.text() on unknown inputreadText(res, { maxBytes })
line.startsWith('data: ')parseSSE(stream)
Regexes over 127., 10., 192.168.assertPublicUrl(url, { lookup })

#Stability

  • Version: 1.0.0
  • Tier: beta
  • Contract: evolving

#Deep dives

  • For agents β€” full export list.
  • Decision record: ADR-0037 (docs/architecture/adrs/0037-net-package.md).

Explore nearby