Vai al contenuto principale

Documentation Index

Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt

Use this file to discover all available pages before exploring further.

Gestisci i limiti di velocità controllando la concorrenza prima che le richieste escano dalla tua app. Quando CometAPI restituisce 429, ritenta con exponential backoff e jitter, quindi riduci il traffico a raffica se si verificano ritenti ripetuti.

Limita la concorrenza

Il seguente esempio Python limita le richieste chat concorrenti con un semaforo async:
import asyncio
import os
from openai import AsyncOpenAI

client = AsyncOpenAI(
    api_key=os.environ["COMETAPI_KEY"],
    base_url="https://api.cometapi.com/v1",
)

semaphore = asyncio.Semaphore(5)

async def ask(prompt):
    async with semaphore:
        completion = await client.chat.completions.create(
            model="your-model-id",
            messages=[{"role": "user", "content": prompt}],
        )
        return completion.choices[0].message.content

async def main():
    prompts = ["Say hello.", "Write a title.", "Return one JSON key."]
    results = await asyncio.gather(*(ask(prompt) for prompt in prompts))
    print(results)

asyncio.run(main())
Il risultato è un array di output del modello:
[
  "Hello.",
  "A concise title",
  "{\"key\":\"value\"}"
]

Ritenta i rate limit

Il seguente esempio JavaScript ritenta le risposte 429 con jitter:
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.COMETAPI_KEY,
  baseURL: "https://api.cometapi.com/v1",
});

async function sleep(milliseconds) {
  return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

async function createCompletion() {
  for (let attempt = 0; attempt < 5; attempt += 1) {
    try {
      return await client.chat.completions.create({
        model: "your-model-id",
        messages: [{ role: "user", content: "Say hello." }],
      });
    } catch (error) {
      if (error.status !== 429 || attempt === 4) {
        throw error;
      }

      const delay = Math.min(30000, 1000 * 2 ** attempt);
      await sleep(delay + Math.random() * 1000);
    }
  }
}

const completion = await createCompletion();
console.log(completion.choices[0].message.content);
La risposta riuscita contiene un normale completamento chat:
{
  "choices": [
    {
      "message": {
        "content": "Hello."
      }
    }
  ]
}

Errori comuni

ErroreCorrezione
Richieste parallele illimitateAggiungi un semaforo, una coda o un pool di worker.
Ritentare tutti gli erroriRitenta solo 429 e gli errori temporanei del server.
Nessuna metrica per modelRegistra route, model ID, stato e latenza per ogni richiesta.
Tempesta di retryAggiungi jitter e limita il ritardo massimo dei retry.
Last modified on May 28, 2026