Skip to main content

Overview

The Chat API provides access to powerful language models for conversational AI, content generation, and text completion tasks. Built for developers who need reliable, scalable AI solutions with streaming responses.
All responses are streamed in real-time, providing a better user experience for conversational applications.

Base URL

Authentication

All requests require a valid API key in the Authorization header:

Request Format

Required Headers

Request Body

Array<Message>
required
Array of conversation messages. Must contain at least one message.
string
default:"llama-3.3-70b-versatile"
AI model to use for generating responses. Optional parameter.

Message Object

Each message in the messages array must contain:
string
required
The role of the message sender. Must be one of:
  • user - Messages from the user/human
  • assistant - Previous AI responses
  • system - System instructions to guide AI behavior
string
required
The actual message content. Cannot be empty.

Available Models

More models will be available soon! Check back for updates on specialized models.

Response Format

The API returns a streaming text response with the following headers:

Response Body

The response is streamed as text chunks. Concatenate all chunks to get the complete AI response.

Examples

Basic Chat Request

Multi-turn Conversation

With System Message

Specifying Model

Streaming Response Handling

The API returns responses as a stream of text chunks. Here’s how to handle streaming in different languages:

Token Usage and Billing

Input Tokens

Counted based on the total length of all messages in your request

Output Tokens

Counted based on the length of the AI’s response

Usage Tracking

Token usage is tracked and counted toward your workspace limits

Optimization

Monitor usage through workspace analytics to optimize costs

Token Estimation

Roughly 4 characters = 1 token for English text. The API uses the same tokenization as the underlying model for precise counting.
Example calculation:

Common Use Cases

Content Generation

Code Assistance

Error Handling

Common Errors

Causes:
  • API key doesn’t exist or has been deleted
  • Incorrect API key format
  • Missing Authorization header
Solutions:
  • Verify your API key is correct and active
  • Check the Authorization header format: Bearer spj_ai_...
  • Generate a new API key if necessary
Cause: Request body doesn’t include a messages arraySolution: Ensure your request includes a valid messages array with at least one message
Causes:
  • Message missing required content field
  • Empty content string
  • Invalid role value
Solution: Ensure all messages have valid role and non-empty content fields
Causes:
  • Workspace has exceeded monthly token quota
  • Request is too large
Solutions:
  • Wait for monthly token reset
  • Upgrade subscription plan
  • Optimize prompts to use fewer tokens
Cause: Making requests too quicklySolutions:
  • Implement exponential backoff
  • Reduce request frequency
  • Use batch processing for multiple prompts
Causes:
  • AI model temporarily unavailable
  • Server overload
  • Temporary service disruption
Solution: Implement retry logic with exponential backoff

Best Practices

Message Design

Clear Instructions

Use specific, clear prompts for better results. Be explicit about what you want.

System Messages

Use system messages to set context and guide AI behavior for specialized tasks.

Conversation Context

Include relevant conversation history, but keep it concise to manage token usage.

Structured Prompts

Break complex requests into clear, structured instructions.

Performance Optimization

Stream responses to provide better user experience in conversational applications.
Always implement proper error handling and retry logic.
Track token usage to stay within limits and optimize costs.

Security Considerations

Never expose API keys in client-side code. Always use backend proxies for frontend applications.

Input Validation

Validate and sanitize user inputs before sending to the API

Content Filtering

Implement content filtering for user-generated prompts

Rate Limiting

Implement application-side rate limiting to prevent abuse

Monitoring

Monitor API usage patterns for unusual activity

Rate Limits

For detailed information about rate limits, token usage, and optimization strategies, see the Rate Limits guide.
  • Token-based limits: Usage counts toward workspace token quotas
  • Request rate: Standard rate limiting applies to prevent abuse
  • Concurrent requests: Multiple simultaneous requests are supported
  • Fair usage: Excessive usage may be throttled

Next Steps

Code Examples

View complete implementation examples in JavaScript, Python, and cURL

Rate Limits

Learn about token usage, optimization, and billing

Error Handling

Comprehensive guide to error codes and recovery patterns

API Reference

Complete API reference with schemas and interactive examples