openai / openai/openai-java

Optimize JSON property order for prompt caching

Open
#316 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement sdk
Dominant language
Kotlin
Stars
1.5k
Forks
264
Avg merge
9h 46m
Merged PRs (30d)
96

Description

Context/motivation: ability to reduce costs while using the APIs via this SDK.

Image

In order to increase the chances of prompts hitting the cache, OpenAI suggests the following:

Image

As far as I understand, and based on experimentation and monitoring, caching works for the entirety of the content passed onto the LLM, and the structure of this content is inherited from the structure of the request body JSON.

While debugging the SDK I have found out that there is no straightforward way to control the structure of Responses API requests.

Example:

  • Developer message (static)
  • User message (static)
  • User message (dynamic)
  • Structured outputs schema (static)

Alternative structure to maximize the probability of cache hits:

  • Structured outputs schema (static)
  • Developer message (static)
  • User message (static)
  • User message (dynamic)

I have tried to call .text() function on ResponseCreateParams after everything else but it has no effect on the resulting request body.

Can there be a workaround or even functionality for this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing ResponseCreateParams and the effect of calling its .text() function when constructing Responses API request bodies. Define how callers would control property ordering, then verify that the resulting JSON places static content before dynamic content and supports the stated prompt-caching goal.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.