Skip to main content

Installation and Setup

The ABV client library comes as a single npm package that includes everything you need. Install it in your project with npm, yarn, or pnpm:
If you’re working with TypeScript, the type definitions are included in the package automatically. You don’t need to install separate @types packages. For projects that want explicit control over tracing behavior, you can also install the tracing package, though it’s already included as a dependency of the client:

Client Initialization Patterns

The way you initialize the ABV client affects how you use it throughout your application. Let’s explore different patterns and when to use each. The simplest initialization creates a client with your API key directly:
This works for quick prototypes or scripts, but hardcoding credentials isn’t recommended for production applications. Instead, use environment variables:
The client also checks for the ABV_API_KEY environment variable automatically, so you can simplify this further:
This pattern is cleaner and makes it impossible to accidentally commit credentials to version control. For applications that need to support multiple regions, specify the region during initialization:
Region selection affects which ABV infrastructure your requests route through. Choose the region closest to your users or the region that matches your data residency requirements.

Client Lifecycle and Reuse

Creating an ABV client is lightweight, but you should generally create one client instance and reuse it throughout your application. The client maintains connection pooling and handles request optimization internally. For a Node.js server application, create the client once at startup and export it for use across your application:
Then import this client wherever you need it:
This pattern ensures efficient connection management and simplifies testing since you can mock the exported client in your test files.

Working with Types

TypeScript’s type system helps catch errors before runtime. The ABV client library provides comprehensive type definitions that make working with the gateway safer and more productive. Import types from the client package to annotate your code:
These types help you build functions that work with gateway responses:
When building the parameters object for a request, TypeScript ensures you provide valid values:
If you make a typo in the provider name or forget a required field, TypeScript flags it immediately. This catches bugs during development rather than in production.

Handling Streaming Responses

Streaming responses arrive as an asynchronous iterable, which you consume using a for-await-of loop. This pattern feels natural in modern JavaScript and TypeScript:
Notice the optional chaining (?.) when accessing the content. Early chunks might not have content, or the structure might vary slightly. Optional chaining prevents runtime errors from null or undefined values. For applications that need to both display tokens as they arrive and store the complete response, accumulate chunks while streaming:
This pattern works well for chatbots that display streaming responses to users but also need to save conversation history.

Error Handling Strategies

Gateway requests can fail for various reasons, and handling these failures gracefully improves your application’s reliability. TypeScript’s type system helps you handle errors correctly. The most basic error handling uses try-catch:
For production applications, you want more sophisticated error handling that distinguishes between error types. Errors might be network issues, rate limits, authentication problems, or invalid requests. Each type suggests different handling:
For applications that need retry logic, implement exponential backoff. This pattern waits increasingly longer between retries, reducing load on the service:
This retry logic handles transient failures like network blips while avoiding endless loops on permanent failures.

Building Conversation Context

Most AI applications involve multi-turn conversations where the model needs context from previous exchanges. Managing this context is a core part of implementing chat applications. The straightforward approach maintains an array of messages that grows with the conversation:
This pattern encapsulates conversation state and ensures the message history stays synchronized. Using it looks like this:
Each message sees the full conversation history, allowing the model to reference earlier exchanges and maintain context. For long-running conversations, you might need to manage the context window size. Here’s a more sophisticated approach that limits history:
This approach prevents the conversation from exceeding token limits by keeping only recent messages. The tradeoff is that the model loses access to earlier context.

Implementing Function Calling

Function calling lets models invoke functions you define, enabling structured outputs and tool use. While the gateway supports function calling, the implementation details vary by provider. This example shows the general pattern:
This pattern defines functions the model can call, parses the model’s function call requests, executes the functions, and returns results to the model.

Framework Integration

The gateway integrates naturally with popular TypeScript frameworks. Here are patterns for common frameworks. For Express.js applications, add an endpoint that handles AI requests:
For Next.js API routes, the pattern is similar but uses Next.js request handlers:
For Next.js applications that need streaming responses to the browser, use server-sent events:

Testing Strategies

Testing code that calls AI models requires different strategies than testing deterministic functions. You can’t assert exact outputs since model responses vary, but you can test the structure of your code and ensure error handling works correctly. Mock the ABV client for unit tests to avoid making actual API calls:
For integration tests where you want to verify actual API behavior, make real requests but structure tests to be flexible about specific outputs:
This approach verifies that the API integration works without depending on specific model outputs.

Next Steps

You now understand how to implement the gateway in TypeScript applications, handle errors, manage conversations, and integrate with frameworks. Here’s where to go next:

Python Guide

Learn how to implement the gateway in Python applications

Available Models

See all supported providers and models with pricing

LLM Gateway Overview

Understand the core concepts and architecture of the gateway

Quickstart

Get up and running with your first gateway request in 5 minutes