Batch Prompting: Efficient Inference with Large Language Model API s (2023)

Abstract: Performing inference on large volumes of samples with large language models (LLMs) can be computationally and financially costly in industry and real-world use. We propose batch prompting, a simple yet effective prompting approach that enables the LLM to run inference in batches, instead of one sample at a time. Our method reduces both token and time costs while retaining downstream performance. We theoretically demonstrate that under a few-shot in-context learning setting, the inference costs decrease almost inverse linearly with the number of samples in each batch. We extensively validate the effectiveness of batch prompting on ten datasets across commonsense QA, arithmetic reasoning, and NLI/NLU: batch prompting significantly (up to 5\times with six samples in batch) reduces the LLM (Codex) inference token and time costs while achieving better or comparable performance. For state-of-the-art Chat-based LLMs, e.g., GPT-3.5 and GPT-4, we show the benefits of batch prompting also hold. Further analysis shows that the number of samples in each batch and the complexity of tasks affect its performance. Moreover, batch prompting can be applied across different reasoning methods using LLMs. Our code is released at the site https://github.com/xlang-ai/batch-prompting.

## introduction large language models (llms) have shown their strong capabilities under zero/few-shot settings with in-context learning @xcite @xcite . much recent work has made progress in incontext learning by eliciting reasoning steps @xcite @xcite @xcite , selecting representative in-context exemplars @xcite @xcite , and designing prompt templates @xcite @xcite . ## approach we first introduce batch prompting, an efficient alternative to standard prompting. we then compare the token and time costs of batch and standard prompting, demonstrating the efficiency of our method. ## experiments we extensively evaluate batch prompting across ten diverse datasets. our results suggest that batch prompting can achieve at most 5× token and time efficiency (with six samples in batches) improvement with similar or even better downstream performance. ## analysis in this section, we assess factors influencing batch prompting performance and the tradeoff between costs and performance. we also demonstrate that batch prompting can be applied to various llm prompting methods, such as end-to-end and code generation. ## limitation batch prompting has proven to be an efficient method for time and token reduction. nonetheless, there are several critical considerations to keep in mind when implementing it across various scenarios. first, to optimize its benefits, the length of the input prompt tokens should be (significantly) greater than that of the output tokens. thus, it might not be suitable for "heavy output" tasks like story generation. it is important to note that while our experiments are conducted with few-shot incontext learning, this method is also applicable to the instruction-following paradigm, either on its own or in combination, by simply substituting or adding the few-shot inputs with instructions. the only crucial factor is the length of the shared input tokens of inference samples. secondly, it is possible to observe performance declines. our experiments indicate that task complexity and lengthy input contexts can negatively impact performance. although we have not identified a definitive guideline for predicting performance, we advise users to initiate testing with a smaller subset to gauge the effectiveness of batch prompting before implementing it on a larger scale. ## conclusion we present batch prompting, a new way to prompt llms that performs inference on samples in a batched fashion. with batch prompting, multiple samples can be handled in one api call so that the costs of tokens and time can be significantly reduced. extensive experiments on ten datasets across commonsense qa, arithmetics, and nli/nlu show that batch prompting can achieve better or similar performance compared to standard prompting, with much lower token and time costs. we hope batch prompting offers pragmatic value to efficient real-world llm usage.