Skip to main content
The agent runtime supports custom transport layers for connecting to different backends, including HTTP proxies and custom streaming implementations.

Overview

By default, the agent calls LLM APIs directly using @mariozechner/pi-ai. You can override this with a custom streamFn to:
  • Route requests through a backend proxy
  • Add authentication or rate limiting
  • Mock LLM responses for testing
  • Implement custom protocols

Proxy Usage

For browser apps that need to proxy through a backend:

Proxy Protocol

The streamProxy function sends requests to your backend with: Request:
Response: Server-sent events (SSE) with AssistantMessageEvent objects

Custom Stream Function

Implement a custom streaming function for full control:

Mock Transport for Testing

Create a mock transport that returns predefined responses:

Dynamic API Keys

Resolve API keys dynamically for OAuth token refresh:

Session IDs

Provide a session ID for provider-side prompt caching:
Session IDs enable prompt caching with OpenAI Codex and other providers that support session-based caching.

Backend Example (Node.js)

Here’s a complete backend proxy example:

Next Steps

State Management

Managing agent state and queues

API Reference

Transport interface documentation