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)
Thecontent 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
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 normallywarn- Request proceeds with warning loggedsoft_block- Request proceeds with modifications appliedhard_block- Request rejected with 403 error
Content Evaluation (Pro+)
When content policies are configured (PII detection, term filtering), the response includes content evaluation details:
Content Results:
allow- Content passes all checkswarn- Content flagged but returnedredact- PII/terms redacted from output (e.g.,john@example.com→[REDACTED])block- Content blocked, error returned
ssn- Social Security Numberscredit_card- Credit card numbers (with Luhn validation)email- Email addressesphone_us- US phone numbersip_address- IP addressesdate_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
- Image Generation API - DALL-E, Stable Diffusion, Flux, and more
- Video Generation API - Veo, Runway, Luma, Pika
- Audio API - Text-to-speech and transcription
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
- Upload a document via
POST /v1/documents(see Documents API) - Wait for the document status to become
ready - Pass the
document_idin your chat request - Perf retrieves the document content and includes it as context
Single Document
Multiple Documents
Document + Schema (Structured Extraction)
Combinedocument_id with schema_id to extract structured data from documents. Upload a PDF, define a schema, and get validated JSON back.
Document Error Responses
Cost Control
Budget Enforcement
When you setmax_cost_per_call, Perf will:
- Estimate the cost for the optimal model
- If estimated cost > budget, select a cheaper alternative
- Process with the selected model
- Include a
cost_warningin theperfobject 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:- Retry with the same model (max 1 retry)
- If still failing, escalate to fallback model
- Return best available result
Multi-Turn Conversations
Include conversation history in themessages array:
- 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
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: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
Related Endpoints
- Schema Enforcement - Validate and auto-repair LLM outputs
- Tools API - Documents/RAG, web search, and memory
- Streaming API - For real-time responses
- Metrics API - For analytics and monitoring
- Logs API - For debugging and audit trails
Support
- Documentation: docs.withperf.pro
- Email: support@withperf.pro
- Status: status.withperf.pro