Use the LangCache API and SDK

Learn to use the Redis LangCache API for semantic caching.

Use the LangCache API from your client app to store and retrieve LLM, RAG, or agent responses.

You can use any standard REST client or library to access the API. If your app is written in Python or Javascript, you can also use the LangCache Software Development Kits (SDKs) to access the API:

Authentication

To access the LangCache API, you need:

  • LangCache API base URL
  • LangCache service API key
  • Cache ID

When you call the API, you need to pass the LangCache API key in the Authorization header as a Bearer token and the Cache ID as the cacheId path parameter.

For example:

This example expects several variables to be set in the shell:

  • $HOST - the LangCache API base URL
  • $CACHE_ID - the Cache ID of your cache
  • $API_KEY - The LangCache API token

Examples

Search LangCache for similar responses

Use POST /v1/caches/{cacheId}/entries/search to search the cache for matching responses to a user prompt.

Place this call in your client app right before you call your LLM's REST API. If LangCache returns a response, you can send that response back to the user instead of calling the LLM.

If LangCache does not return a response, you should call your LLM's REST API to generate a new response. After you get a response from the LLM, you can store it in LangCache for future use.

You can also scope the responses returned from LangCache by adding an attributes object to the request. LangCache will only return responses that match the attributes you specify.

Store a new response in LangCache

Use POST /v1/caches/{cacheId}/entries to store a new response in the cache.

Place this call in your client app after you get a response from the LLM. This will store the response in the cache for future use.

You can also store the responses with custom attributes by adding an attributes object to the request.

Delete cached responses

Use DELETE /v1/caches/{cacheId}/entries/{entryId} to delete a cached response from the cache.

You can also use DELETE /v1/caches/{cacheId}/entries to delete multiple cached responses based on the attributes you specify. If you specify multiple attributes, LangCache will delete entries that contain all given attributes.

Warning:
If you do not specify any attributes, all responses in the cache will be deleted. This cannot be undone.