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 Context Cache 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 Doubao-1.5-pro-32k. Doubao-1.5-pro-32k
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
mode string No Context cache type. See the official documentation for Context Caching (Context API) Overview. Supported values are session cache session and prefix cache common_prefix. session
ttl number No TTL in seconds. The timer starts when the cache is created and resets each time the cache is used. When the TTL expires, the cached information is removed. The allowed range is 1 hour to 7 days, i.e. [3600, 604800]. 86400

4. Request Examples

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

{
    "model": "Doubao-1.5-pro-32k",
    "messages": [
        {
            "role": "system",
            "content": "Please act as a friendly customer support agent"
        },
        {
            "role": "user",
            "content": "Hello"
        }
    ],
    "mode":"session",
    "ttl": 3600
}
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\": \"Doubao-1.5-pro-32k\",
    \"messages\": [{
            \"role\": \"system\",
            \"content\": \"Please act as a friendly customer support agent\"
        },
        {
            \"role\": \"user\",
            \"content\": \"Hello\"
        }
    ],
    \"mode\": \"session\",
    \"ttl\": 3600
}"
package main

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

const (
    YOUR_API_KEY    = "sk-123456789012345678901234567890123456789012345678"
    REQUEST_PAYLOAD = `{
    "model": "Doubao-1.5-pro-32k",
    "messages": [{
            "role": "system",
            "content": "Please act as a friendly customer support agent"
        },
        {
            "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": "ctx-20241211104333-12345",
    "ttl": 3600,
    "truncation_strategy": {
        "type": "rolling_tokens",
        "rolling_tokens": true
    },
    "usage": {
        "prompt_tokens": 8,
        "completion_tokens": 0,
        "total_tokens": 8,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    },
    "mode": "session"
}