AI API

Stream AI chat completions through the Proma gateway. Credits deducted per token.

The AI API is available at proma.ai. It requires the ai:chat scope. Credits are deducted automatically based on tokens used — an estimated amount is charged upfront and the difference is refunded after the stream completes.

Supported models: gemini-2.0-flash (default), gemini-1.5-flash, gemini-1.5-pro

chat

Sends a chat request and returns a streaming Response. Iterate the SSE stream for real-time output.

const stream = await proma.ai.chat([
  { role: 'system', content: 'You are a helpful assistant.' },
  { role: 'user',   content: 'Explain how OAuth2 PKCE works.' },
])

const reader = stream.body.getReader()
const decoder = new TextDecoder()

while (true) {
  const { done, value } = await reader.read()
  if (done) break
  process.stdout.write(decoder.decode(value))
}

You can also pass a ChatOptions object to specify the model:

const stream = await proma.ai.chat({
  messages: [{ role: 'user', content: 'Hello!' }],
  model: 'gemini-1.5-pro',
})

Parameters

interface ChatOptions {
  messages: ChatMessage[]
  model?: 'gemini-2.0-flash' | 'gemini-1.5-flash' | 'gemini-1.5-pro'
}

interface ChatMessage {
  role: 'user' | 'assistant' | 'system'
  content: string
}

chatText

Convenience wrapper that collects the full streamed response and returns it as a string. Use this when you don't need real-time streaming.

const text = await proma.ai.chatText({
  messages: [{ role: 'user', content: 'Say hello in Spanish.' }],
  model: 'gemini-2.0-flash',
})

console.log(text) // "¡Hola!"

Or pass the messages array directly:

const text = await proma.ai.chatText([
  { role: 'user', content: 'What is 2 + 2?' }
])

Credit consumption

Credits are pre-deducted based on an estimate before the request is sent. After the stream completes, the actual token usage reported by Gemini is used to compute the final cost and any overpayment is refunded.

Approximate rates:

  • 1 input token = 1 micro-credit
  • 1 output token = 2 micro-credits

A typical short exchange costs 1,000–5,000 micro-credits ($0.001–$0.005).