Latency Optimization

A guide to principles for improving latency across a wide variety of LLM-related use cases, grouped into 7 principles.

Verified

On this page

Definition

The guide covers a core set of principles for improving latency across a wide variety of LLM-related use cases. It groups the individual techniques into 7 principles that form a high-level taxonomy of latency improvements.

At a glance

The example analyzes the architecture and prompts of a hypothetical customer service bot inspired by real production applications. The diagram summarizes the bot’s process.

Diagram: A user sends a message as part of an ongoing conversation. > The last message is turned into a self-contained query. > The bot determines whether additional (retrieved) information is required to respond to that query. > Retrieval is performed, producing search results. > The assistant reasons about the user's query and the search results, and produces a response. > The response is sent back to the user.1A user sends a message as part of an ongoingconversation.2The last message is turned into aself-contained query.3The bot determines whether additional(retrieved) information is required to respondto that query.4Retrieval is performed, producing searchresults.5The assistant reasons about the user's queryand the search results, and produces aresponse.6The response is sent back to the user.

How it works

The OpenAI guide says model size is the main factor in inference speed: smaller models usually run faster and cost less, and when used correctly they can outperform larger models. The OpenAI guide says Predicted outputs can reduce generation latency when most of the output is known ahead of time, such as in code editing tasks. The OpenAI guide gives this heuristic: generating tokens is almost always the highest-latency step when using an LLM, and cutting 50% of output tokens may cut about 50% of latency.

For structured output, the guide suggests minimizing output syntax where possible by shortening function names, omitting named arguments and coalescing parameters. The OpenAI guide says fewer input tokens reduce latency, but this is not usually a significant factor: cutting 50% of a prompt may improve latency by only 1 to 5%. Each request incurs some round-trip latency, and this can start to add up.

For sequential steps, the guide suggests considering putting them in a single prompt and getting the results in a single response instead of sending one request per step. If the steps are not strictly sequential, they can be split out into parallel calls. Speculative execution may still be possible when the steps are strictly sequential.

In the speculative execution outline, step 1 and step 2 start simultaneously, for example input moderation and story generation. The result of step 1 is verified. If the result was not the expected one, step 2 is cancelled, and retried if necessary.

If output needs moderation, translation or other processing before the user sees it, the guide suggests processing it in chunks. The guide says language models are sometimes used where a classical method would be faster, because language models are powerful and versatile.

Example

The check for retrieval requires the contextualized query, so the guide combines query contextualization and the retrieval check into a single prompt to make fewer requests. In the guide’s hypothetical bot example, the combined prompt uses a smaller, fine-tuned GPT-3.5; the guide groups this change under processing tokens faster. The same example splits the assistant prompt in two and uses a smaller, fine-tuned GPT-3.5 for reasoning; the guide also groups this under processing tokens faster.

Because the reasoning prompt does not depend on the retrieved context, it can be parallelized and fired off at the same time as the retrieval prompts. Making reasoning field names shorter and moving explanations to comments lets the example generate fewer tokens. The guide reports that the field-name change removed 19 output tokens.

Compared with the prompt before this change, the guide estimates an improvement of a few milliseconds with GPT-3.5 and up to a second with GPT-4.

From the official source
Plain text
SYSTEM: Given the previous conversation, re-write the last user query so it contains
all necessary context.

# Example
History: [{user: "What is your return policy?"},{assistant: "..."}]
User Query: "How long does it cover?"
Response: "How long does the return policy cover?"

# Conversation
[last 3 messages of conversation]

# User Query
[last user query]

USER: [JSON-formatted input conversation here]
From the official source
Plain text
SYSTEM: Given a user query, determine whether it requires doing a realtime lookup to
respond to.

# Examples
User Query: "How can I return this item after 30 days?"
Response: "true"

User Query: "Thank you!"
Response: "false"

USER: [input user query here]
From the official source
Plain text
SYSTEM: Given the previous conversation, re-write the last user query so it contains
all necessary context. Then, determine whether the full request requires doing a
realtime lookup to respond to.

Respond in the following form:
{
  query:"[contextualized query]",
  retrieval:"[true/false - whether retrieval is required]"
}

# Examples

History: [{user: "What is your return policy?"},{assistant: "..."}]
User Query: "How long does it cover?"
Response: {query: "How long does the return policy cover?", retrieval: "true"}

History: [{user: "How can I return this item after 30 days?"},{assistant: "..."}]
User Query: "Thank you!"
Response: {query: "Thank you!", retrieval: "false"}

# Conversation
[last 3 messages of conversation]

# User Query
[last user query]

USER: [JSON-formatted input conversation here]
From the official source
Plain text
SYSTEM: You are a helpful customer service bot.

Based on the previous conversation, respond in a JSON to determine the required
fields.

# Example

User: "My freaking computer screen is cracked!"

Assistant Response:
{
  "message_is_conversation_continuation": "True",
  "number_of_messages_in_conversation_so_far": "1",
  "user_sentiment": "Aggravated",
  "query_type": "Hardware Issue",
  "response_tone": "Validating and solution-oriented",
  "response_requirements": "Propose options for repair or replacement.",
  "user_requesting_to_talk_to_human": "False",
}
From the official source
Plain text
SYSTEM: You are a helpful customer service bot.

Use the retrieved context, as well as these pre-classified fields, to respond to
the user's query.

# Reasoning Fields
` ` `
[reasoning json determined in previous GPT-3.5 call]
` ` `

# Example

User: "My freaking computer screen is cracked!"

Assistant Response:
{
  "enough_information_in_context": "True",
  "response": "..."
}

USER: # Relevant Information
` ` `
[retrieved context]
` ` `
From the official source
typescript
{
  cont: "True", // whether last message is a continuation
  n_msg: "1", // number of messages in the continued conversation
  tone_in: "Aggravated", // sentiment of user query
  type: "Hardware Issue", // type of the user query
  tone_out: "Validating and solution-oriented", // desired tone for response
  reqs: "Propose options for repair or replacement.", // response requirements
  human: "False", // whether user is expressing want to talk to human
};

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.