Azure AI SDK for JavaScript

Frequently Asked Questions

What versions of Node.js are supported?

The Azure AI JavaScript SDK supports Node.js LTS versions 14.x, 16.x, and 18.x. For browser environments, the SDK works with any modern browser that supports ES6.

How do I authenticate my client?

You can authenticate using any of the following methods:

  • Azure Active Directory (DefaultAzureCredential)
  • Azure API Key (AzureKeyCredential)
  • Managed Identity (ManagedIdentityCredential)

See the Authentication guide for code snippets.

Can I stream responses from the AI service?

Yes. The SDK provides a stream option that returns an async iterator. This allows you to process tokens as they arrive without waiting for the full response.

const client = new OpenAIClient(...);
for await (const token of client.getChatCompletionsStream(...)) {
    console.log(token);
}
What is the recommended way to handle retries?

The SDK uses the Azure Core RetryPolicy by default with exponential backoff. You can customize retry settings when constructing the client:

const client = new OpenAIClient(endpoint, credential, {
    retryOptions: { maxRetries: 5, retryDelayInMs: 2000 }
});
How do I limit token usage for a request?

Pass the maxTokens parameter in the request options. The service will stop generating once the limit is reached.

const response = await client.getChatCompletions({
    messages: [...],
    maxTokens: 512
});
Is there a way to get usage metrics?

Yes. Set includeUsage to true in the request options, and the response will contain a usage object with token counts.

const result = await client.getChatCompletions({
    messages,
    includeUsage: true
});
console.log(result.usage);
Where can I find the full API reference?

The complete reference is available at the API Reference page, which includes all classes, methods, and enumerations.