Chat API

Complete reference for the Cencori Chat Completions API. Create AI chat interactions with built-in security, logging, and multi-provider support.

Overview

The Chat API provides a unified interface to interact with multiple AI providers (OpenAI, Anthropic, Google) through a single endpoint. Every request is automatically secured, logged, and monitored.

  • Unified Interface: Same API for all providers (OpenAI, Anthropic, Google)
  • Automatic Security: Built-in threat detection and PII filtering
  • Complete Logging: Every request and response is logged
  • Cost Tracking: Token usage and costs calculated automatically

Basic Usage

Create a chat completion with the Cencori SDK:

app/api/chat/route.ts

Request Parameters

The chat.completions.create() method accepts the following parameters:

model (required)

The AI model to use. Supported models include:

  • OpenAI: gpt-4, gpt-4-turbo, gpt-3.5-turbo
  • Anthropic: claude-3-opus-20240229, claude-3-sonnet-20240229
  • Google: gemini-pro, gemini-pro-vision

messages (required)

An array of message objects representing the conversation history. Each message has a role and content.

messages-example.ts

Valid roles: system, user, assistant

temperature (optional)

Controls randomness in responses. Range: 0 to 2. Default: 1.

  • Lower values (0.0-0.3): More focused and deterministic
  • Higher values (1.5-2.0): More creative and random

max_tokens (optional)

Maximum number of tokens to generate in the response. Default varies by model.

top_p (optional)

Nucleus sampling parameter. Range: 0 to 1. Default: 1. Alternative to temperature for controlling randomness.

stream (optional)

If true, responses will be streamed back as they're generated. Default: false.

user (optional)

A unique identifier for the end-user. Useful for monitoring, rate limiting, and abuse detection.

Complete Example

A full example with all common parameters:

app/api/chat/route.ts

Response Format

The API returns a structured response object:

response-example.ts

Response Fields

  • id: Unique identifier for this completion
  • choices: Array of completion choices (usually contains one item)
  • choices[].message: The generated message with role and content
  • choices[].finish_reason: Why generation stopped (stop, length, content_filter)
  • usage: Token counts for the request and response

Streaming Responses

Stream responses in real-time for better user experience:

streaming-example.ts

Multi-Provider Support

Switch between AI providers by simply changing the model name:

provider-switching.ts

Note: Make sure you've added the respective provider API keys in your Cencori project settings.

Error Handling

Handle various error scenarios gracefully:

error-handling.ts

Security Note: For security reasons, detailed detection patterns are not included in error responses. See the Security documentation for comprehensive error handling guide with UI examples.

Advanced Features

Cencori supports advanced AI features through the same unified interface:

Function Calling

Enable the model to call external functions:

function-calling.ts

System Prompts

Set the AI's behavior with system messages:

system-prompt.ts

JSON Mode

Force the model to return valid JSON:

json-mode.ts

Best Practices

Follow these recommendations for optimal performance and reliability:

  • Set max_tokens: Prevent unexpectedly long responses and control costs
  • Include user IDs: Enable per-user rate limiting and better analytics
  • Handle errors gracefully: Implement retry logic for transient failures
  • Use streaming for chat UIs: Provide better user experience with progressive responses
  • Monitor token usage: Check the dashboard regularly to track costs
  • Test with different models: Compare quality and cost across providers
  • Implement timeouts: Don't let requests hang indefinitely
  • Cache responses when appropriate: Reduce costs for repeated queries