Skip to content

Context Cache Creation

1. Overview

Creates a context cache. After obtaining the cache id from this endpoint, pass it as context_id to the Chat Completion endpoint.

2. Request

  • Method:POST
  • Endpoint:

    https://gateway.serevixai.ai/v1/context/create
    

3. Parameters

3.1 Header Parameters

Parameter Type Required Description Example
Content-Type string Yes Sets the request content type. It must be application/json application/json
Accept string Yes Sets the response content type. The recommended value is application/json application/json
Authorization string Yes API key required for authentication, in the format Bearer $YOUR_API_KEY. Bearer $YOUR_API_KEY

3.2 Body Parameters (application/json)

Parameter Type Required Description Example
model string Yes The model ID to use. See Model List for available versions, such as gemini-2.0-flash. gemini-2.0-flash
messages array Yes Messages used to initialize or store information in the cache, in an OpenAI-compatible format. Each item contains role and content. [{"role": "system","content": "you are a helpful asssistant"}]
role string No Message role. Supported values: system, user, and assistant. system
content string No The message content. you are a helpful asssistant
ttl number No TTL in seconds. 300

4. Request Examples

POST /v1/context/create
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY

{
    "model": "gemini-2.0-flash",
    "messages": [
        {
            "role": "system",
            "content": "Please act as a friendly customer support agent.... at least 4096 tokens ..."
        },
        {
            "role": "user",
            "content": "Hello"
        }
    ],
    "ttl": 300
}
curl https://gateway.serevixai.ai/v1/context/create \
    -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    -H "Authorization: Bearer $YOUR_API_KEY" \
    -d "{
    \"model\": \"gemini-2.0-flash\",
    \"messages\": [{
            \"role\": \"system\",
            \"content\": \"Please act as a friendly customer support agent.... at least 4096 tokens ...\"
        },
        {
            \"role\": \"user\",
            \"content\": \"Hello\"
        }
    ],
    \"ttl\": 300
}"
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "strings"
)

const (
    YOUR_API_KEY    = "sk-123456789012345678901234567890123456789012345678"
    REQUEST_PAYLOAD = `{
    "model": "gemini-2.0-flash",
    "messages": [{
            "role": "system",
            "content": "Please act as a friendly customer support agent.... at least 4096 tokens ..."
        },
        {
            "role": "user",
            "content": "Hello"
        }
    ],
    "mode": "session",
    "ttl": 3600
}`
)

func main() {

    requestURL := "https://gateway.serevixai.ai/v1/context/create"
    requestMethod := "POST"
    requestPayload := strings.NewReader(REQUEST_PAYLOAD)

    req, err := http.NewRequest(requestMethod, requestURL, requestPayload)
    if err != nil {
        fmt.Println("Create request failed, err: ", err)
        return
    }

    req.Header.Add("Content-Type", "application/json")
    req.Header.Add("Accept", "application/json")
    req.Header.Add("Authorization", "Bearer "+YOUR_API_KEY)

    client := &http.Client{}

    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Do request failed, err: ", err)
        return
    }
    defer resp.Body.Close()

    respBodyBytes, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Read response body failed, err: ", err)
        return
    }
    fmt.Println(string(respBodyBytes))
}

5. Response Example

{
    "id": "projects/37021971161/locations/global/cachedContents/1692176707571679232",
    "ttl": 300,
    "usage": {
        "prompt_tokens": 4375,
        "completion_tokens": 0,
        "total_tokens": 0,
        "cache_creation_input_tokens": 4375
    }
}