Technical architecture and API

Updated at:

This topic describes the internal request processing workflow of Tair AI Gateway and the complete API specifications, configuration parameters, and error codes for two semantic cache modes.

Request processing workflow

image

OpenAI-compatible mode

The following steps describe the complete request processing workflow in OpenAI-compatible mode:

  1. Request ingestion — Receives an OpenAI-compatible request.

  2. Key extraction — Extracts a cache key from the request JSON by using a GJSON PATH expression. By default, the last user message is extracted.

  3. Exact match — Searches Tair for an identical cache entry. On a hit, the cached response is returned immediately with a latency of approximately 1 to 2 ms.

  4. Embedding — On an exact match miss, calls an embedding model to vectorize the text.

  5. Semantic search — Performs a KNN approximate search in Tair Vector.

  6. Similarity check — If the most similar result meets the threshold, the cached response is returned with a latency of approximately 5 to 20 ms.

  7. LLM fallback — On no match, forwards the request to the Model Studio (Bailian) platform for inference.

  8. Dynamic threshold adjustment — Applies only in dynamic threshold mode. The system samples requests to check whether cached answers are consistent with LLM answers, and automatically adjusts the similarity threshold: it raises the threshold when the error risk increases and lowers it when the risk is low.

  9. Cache write — Automatically writes the LLM response to Tair so that subsequent identical or similar requests can reuse it.

LangCache-compatible mode

LangCache mode provides only cache create, delete, and query capabilities. It does not proxy LLM calls. Developers use the API to actively manage cache entries and decide when to query the cache, call an LLM, and write entries.

OpenAI-compatible mode API

This mode is based on the OpenAI protocol and requires no additional API learning. Replace the Base URL and API Key in your existing code with the service address and API Key of your AI Gateway instance.

Chat Completions API

When to use: Standard multi-turn conversation scenarios. Compatible with all OpenAI Chat format requests.

POST /compatible-mode/v1/chat/completions

Example:

curl -X POST https://<Base URL>/compatible-mode/v1/chat/completions \
  -H "Authorization: Bearer <API Key>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6-plus",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "What is semantic caching?"}
    ]
  }'

Responses API

When to use: Simple single-turn Q&A scenarios where maintaining a messages array is not needed.

POST /compatible-mode/v1/responses

Example:

curl -X POST https://<Base URL>/compatible-mode/v1/responses \
  -H "Authorization: Bearer <API Key>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6-plus",
    "input": "Briefly explain how semantic caching works"
  }'

Model Studio (Bailian) DashScope API

When to use: Applications that already use a DashScope SDK and do not want to switch to the OpenAI format.

POST /api/v1/services/aigc/text-generation/generation

Example:

curl -X POST https://<Base URL>/api/v1/services/aigc/text-generation/generation \
  -H "Authorization: Bearer <API Key>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6-plus",
    "input": {
      "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is vector search?"}
      ]
    },
    "parameters": {"result_format": "message"}
  }'

Anthropic Messages API

When to use: Requests that already use the Anthropic Messages API format, or scenarios where you want to access models through the Anthropic protocol.

POST /compatible-mode/v1/messages

Example:

curl -X POST https://<BaseURL>/v1/messages \
  -H "x-api-key: <API_KEY>" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.7-max",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": "Hello, could you provide an overview of semantic caching?"
      }
    ]
  }'

SDK integration

Integrate by using the following SDKs. For installation instructions, see Install the SDK:

  • OpenAI Python SDK / OpenAI Node.js SDK

  • DashScope Python SDK / DashScope Java SDK

Note

During public preview, only qwen3.6-plus is supported as the model.

Core configuration parameters

Parameter

Type

Description

Default

cache_strategy

String

Cache strategy: exact or exact_and_semantic

exact_and_semantic

similarity_threshold

Float

Similarity threshold for semantic matching in fixed threshold mode. Valid values: 0 to 1. This parameter does not take effect in dynamic threshold mode.

0.85

attributes

Array

Cache isolation attributes. Each item contains name, source, and path, which are used to extract attribute values from the request header or request body.

[{"name":"model","source":"body","path":"model"}]

decision_policy

String

Semantic cache decision policy: static (fixed threshold) or dynamic (dynamic threshold). Takes effect only when cache_strategy is set to exact_and_semantic.

static

dynamic.error_rate_bound

Float

Maximum cache error rate allowed in dynamic threshold mode. Valid values: (0, 1). A smaller value makes cache hit decisions more conservative. Takes effect only when decision_policy is set to dynamic.

0.01

dynamic.match_strategy

String

Answer consistency check strategy in dynamic threshold mode, used to check whether a cached answer is consistent with the LLM answer: normalized, strict, f1, or llm.

f1

dynamic.f1_threshold

Float

Minimum F1 score for two answers to be considered consistent when dynamic.match_strategy is set to f1. Valid values: (0, 1]. Takes effect only when decision_policy is set to dynamic.

0.85

cache.ttl

Integer

TTL for exact cache entries in seconds. 0 = never expires.

0

vector.ttl

Integer

TTL for vector indexes in seconds. 0 = never expires.

0

LangCache-compatible mode API

The base path for all APIs is /v1/caches/{cacheId}, where cacheId is the bound Tair instance ID.

Semantic search

When to use: Query the cache for a semantically similar existing response. This is the core operation of LangCache mode — search first, use the cache on a hit, or call the LLM on a miss.

POST /v1/caches/{cacheId}/entries/search

Request parameters

Parameter

Type

Required

Description

prompt

String

Yes

The search text. This text is embedded and then matched.

similarityThreshold

Float

No

Similarity threshold for this search. Overrides the global configuration.

attributes

Object

No

Metadata filter conditions, such as filtering by language or topic.

searchStrategies

Array

No

Search strategies, such as ["exact", "semantic"].

Response example

{
  "data": [
    {
      "id": "5b84acef3ce360988d1b35adbaaaccb1",
      "prompt": "Tell me how semantic caching works",
      "response": "Semantic caching stores and retrieves data based on meaning rather than exact string matching.",
      "attributes": {"language": "en", "topic": "cache"},
      "similarity": 0.95,
      "searchStrategy": "semantic"
    }
  ]
}

If no results are found, data is an empty array.

Insert cache entry

When to use: After receiving an LLM response, store the prompt-response pair in the cache for future reuse.

POST /v1/caches/{cacheId}/entries

Request parameters

Parameter

Type

Required

Description

prompt

String

Yes

The cache key (original prompt text).

response

String

Yes

The cache value (LLM response).

attributes

Object

No

Metadata tags for future search filtering.

ttlMillis

Integer

No

Expiration time in milliseconds. Overrides the global TTL configuration.

Response

{
  "entryId": "a1b2c3d4e5f6..."
}

Delete cache entry

When to use: A cache entry is outdated or incorrect and needs to be removed individually.

DELETE /v1/caches/{cacheId}/entries/{entryId}

Returns HTTP 204 on success with no response body.

Flush cache

When to use: Rebuild the entire cache, such as after a model upgrade when old answers are no longer applicable.

POST /v1/caches/{cacheId}/flush

Returns HTTP 204 on success.

Warning

This operation is irreversible and clears all cached data under the instance.

Core configuration parameters

Parameter

Type

Description

Default

ttl_millis

Integer

Global cache TTL in milliseconds. 0 = never expires.

0

similarity_threshold

Float

Global similarity threshold.

0.85

search_strategies

Array

Default search strategies, such as ["exact","semantic"] (exact first, then semantic).

["exact","semantic"]

SDK integration

SDK

Language

Installation

LangCache SDK for Python

Python

pip install langcache (PyPI)

LangCache SDK for JavaScript

JavaScript

npm install @redis-ai/langcache (npm)

Error codes

Status code

Description

Solution

400

Invalid Request — incorrect request parameters.

Fix the request format on the client side.

403

Incorrect API key provided — invalid API key.

Fix the API Key in the request on the client side.

404

Not Found — cache instance or entry does not exist.

Fix the request path or the cache ID in the path on the client side.

500

Internal Server Error — server-side internal error.

Model Studio (Bailian) or Tair request failed. Retry the operation.

All error responses use the following format:

{
  "title": "Error title",
  "status": 400,
  "detail": "Detailed error description",
  "instance": "/v1/caches/xxx/entries/search",
  "type": "/errors/invalid-data"
}