Skip to main content

Chat API Reference

The Chat API is Perf’s primary endpoint for text generation. It automatically routes your request to the optimal model based on task type, complexity, and your cost constraints. The response format is OpenAI-compatible, making it easy to integrate with existing applications.

Endpoint

Authentication

Include your API key in the Authorization header:

Request Body

Required Parameters

Optional Parameters

Message Object

Multimodal Content (Vision)

The content field can be a string for text-only messages, or an array of content parts for multimodal messages (images, audio, video, documents).

Content Part Types

Vision Request Example

Base64 Image Example

Perf automatically routes vision requests to models with image understanding capabilities (GPT-4o, Claude 3.5 Sonnet, Gemini Pro Vision).

Request Example

Response

Success Response (200 OK)

The response follows the OpenAI Chat Completion format:

Response Fields

Response Headers

Perf includes additional metadata in response headers:

Policy Evaluation (Pro+)

When routing policies are configured for your project, the response includes policy evaluation details:
Policy Results:
  • allow - Request proceeds normally
  • warn - Request proceeds with warning logged
  • soft_block - Request proceeds with modifications applied
  • hard_block - Request rejected with 403 error
See Policies API for available policy templates and configuration.

Content Evaluation (Pro+)

When content policies are configured (PII detection, term filtering), the response includes content evaluation details:
Content Results:
  • allow - Content passes all checks
  • warn - Content flagged but returned
  • redact - PII/terms redacted from output (e.g., john@example.com[REDACTED])
  • block - Content blocked, error returned
Supported PII Types:
  • ssn - Social Security Numbers
  • credit_card - Credit card numbers (with Luhn validation)
  • email - Email addresses
  • phone_us - US phone numbers
  • ip_address - IP addresses
  • date_of_birth - Dates of birth

Task Types

Perf automatically detects your task type for optimal routing:

Generation Intent Detection

The Chat API intelligently detects when your prompt is requesting media generation (images, video, audio) and automatically routes to the appropriate generation model.

Example

Perf understands this is an image generation request and routes to DALL-E, returning the generated image URL in the response. For more control over generation parameters (model selection, dimensions, quality), use the dedicated generation endpoints:

Document Context

Reference uploaded documents directly in your chat requests. Perf automatically retrieves the document content and injects it as context for the AI model. This is ideal for extracting structured data from PDFs, answering questions about uploaded files, or any task that requires grounding the AI response in specific document content.

How It Works

  1. Upload a document via POST /v1/documents (see Documents API)
  2. Wait for the document status to become ready
  3. Pass the document_id in your chat request
  4. Perf retrieves the document content and includes it as context
For small documents (under ~40 pages), the full content is injected. For larger documents, Perf uses semantic search (RAG) to find and inject the most relevant sections based on your message.

Single Document

Multiple Documents

Document + Schema (Structured Extraction)

Combine document_id with schema_id to extract structured data from documents. Upload a PDF, define a schema, and get validated JSON back.
Response with structured, schema-validated output:

Document Error Responses

Cost Control

Budget Enforcement

When you set max_cost_per_call, Perf will:
  1. Estimate the cost for the optimal model
  2. If estimated cost > budget, select a cheaper alternative
  3. Process with the selected model
  4. Include a cost_warning in the perf object if budget was a factor

Quality Validation

Perf automatically validates outputs and retries if needed:

Validation Checks

  • JSON format correctness (for extraction/classification tasks)
  • Refusal detection (“I cannot assist with that…”)
  • Incomplete response detection

Retry Logic

If validation fails:
  1. Retry with the same model (max 1 retry)
  2. If still failing, escalate to fallback model
  3. Return best available result

Multi-Turn Conversations

Include conversation history in the messages array:
Perf automatically:
  • Summarizes long conversation history to fit context windows
  • Maintains semantic coherence
  • Optimizes for cost by compressing older messages

Error Responses

400 Bad Request

401 Unauthorized

429 Too Many Requests

Response headers:

500 Internal Server Error

503 Service Unavailable

Structured Output

For extraction and classification tasks, Perf automatically detects when JSON output is needed and routes to models that excel at structured output. To get JSON output, simply ask for it in your prompt:
Perf will detect this is an extraction task and route accordingly.

Rate Limits

Best Practices

1. Set Appropriate Budgets

2. Use System Messages

Guide model behavior with system messages:

3. Optimize for Task Type

Be explicit about the task for better routing:

4. Handle Errors Gracefully

SDK Support

Official SDKs coming soon:
  • Python SDK
  • Node.js SDK
  • Go SDK
  • Ruby SDK

Support