Pular para o conteúdo principal

Flex Tier

The Flex Tier offers a 50% discount on synchronous (real-time) requests, subject to capacity availability. When capacity is not immediately available, the request is queued and waits for up to 5 minutes before the API returns HTTP status code 429. If the queue is already full when the request arrives, the API returns 429 immediately.

How to use​

To enable the Flex Tier, include the service_tier: "flex" parameter in your request:

import openai

client = openai.OpenAI(
api_key="your-api-key",
base_url="https://chat.maritaca.ai/api",
)

response = client.chat.completions.create(
model="sabiazinho-4",
messages=[
{"role": "user", "content": "What is the capital of Brazil?"},
],
extra_body={"service_tier": "flex"},
)

print(response.choices[0].message.content)
print(f"Service tier: {response.service_tier}") # "flex"

Pricing​

Flex requests have the same 50% discount as the Batch API, but are processed in real time. See the full pricing table on the Pricing page.

Flex vs Batch API​

FlexBatch API
Discount50%50%
ResponseReal-time (synchronous)Up to 24h (asynchronous)
AvailabilitySubject to capacity (queue up to 5 min, or immediate 429 if queue is full)Guaranteed within 24h window
StreamingYesNo
Best forApplications that tolerate retryNon-urgent batch processing

Best practices​

  1. Combine with prompt caching: cached tokens keep the 75% discount on input price. The Flex discount (50%) applies only to non-cached input tokens and output tokens — the discounts do not multiply. See more details at Prompt Caching.
  2. Implement retry with backoff: since Flex requests may return 429, add retry logic with exponential backoff.
import openai
import time
import random

client = openai.OpenAI(
api_key="your-api-key",
base_url="https://chat.maritaca.ai/api",
)

def chat_flex_with_retry(messages, model="sabiazinho-4", max_retries=5):
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model=model,
messages=messages,
extra_body={"service_tier": "flex"},
)
return response
except openai.RateLimitError:
if attempt == max_retries - 1:
raise
wait = (2 ** attempt) + random.random() # jitter to avoid simultaneous retries
print(f"429 received. Retrying in {wait:.1f}s...")
time.sleep(wait)

response = chat_flex_with_retry(
messages=[{"role": "user", "content": "What is the capital of Brazil?"}]
)
print(response.choices[0].message.content)