completion(): your first call
litellm.completion() is the function that sends a model string and a list of messages to a provider and returns the answer as a ModelResponse object in the OpenAI format.
Last updated: 12 Oct, 2026 · LiteLLM 1.105
Installation and setup made a first call and printed one field. This lesson takes the call apart: what goes in, what comes back, and what happens when the reply limit is too small.
Syntax: completion() needs a model and messages. Every other argument is optional.
response = completion(
model="groq/openai/gpt-oss-120b", # provider, slash, model id
messages=[{"role": "user", "content": "..."}], # the conversation
max_tokens=400, # the most tokens the reply may use
temperature=0, # 0 is the most repeatable setting
)Sending a system message and a user message
Writing the messages list
messages is a list of dictionaries. Each has a role and a content. A system message sets the rules for the whole conversation, and a user message is what the person says.
messages = [
{"role": "system", "content": "You sort support tickets. Reply with one word."},
{"role": "user", "content": "I was charged twice for one order"},
]Running the call
The call below uses the two messages above and prints only the text of the answer. choices[0] is the first answer, because a provider may return several, and message.content is its text.
from litellm import completion
messages = [
{"role": "system", "content": "You sort support tickets. Reply with one word."},
{"role": "user", "content": "I was charged twice for one order"},
]
response = completion(model="groq/openai/gpt-oss-120b", messages=messages, max_tokens=400)
print(response.choices[0].message.content)Billing
Reading the fields of the response
The result is a ModelResponse. Its fields are the same for every provider, which is what lets later code ignore which provider answered.
from litellm import completion
messages = [{"role": "user", "content": "Name one colour."}]
response = completion(model="groq/openai/gpt-oss-120b", messages=messages, max_tokens=400)
print(type(response).__name__)
print(response.model)
print(response.choices[0].message.role)
print(response.choices[0].finish_reason)
print(response.usage.prompt_tokens, response.usage.completion_tokens, response.usage.total_tokens)
print(response["choices"][0]["message"]["content"] == response.choices[0].message.content)ModelResponse openai/gpt-oss-120b assistant stop 75 35 110 True
- ModelResponse is LiteLLM's own class for every answer.
- response.model is the model name the provider reports. For Groq it has no
groq/prefix, because that prefix is LiteLLM's and not Groq's. - message.role is
assistant, the role the model answers in. - finish_reason is
stopwhen the model finished on its own. A different value means it was cut off, as the next section shows. - usage counts the tokens: the prompt going in, the completion coming out, and their total.
completion_tokensincludes any hidden thinking tokens. - The last line is
Truebecause the response also answers square-bracket access, so code written against OpenAI's JSON keeps working.
Why a small max_tokens returns an empty answer
max_tokens is the most tokens the model may write. gpt-oss is a reasoning model: it writes hidden thinking tokens before the answer, and they count against the same limit. Set the limit too low and the model can stop while it is still thinking. This call sets it to 20.
from litellm import completion
messages = [{"role": "user", "content": "Name one colour."}]
response = completion(model="groq/openai/gpt-oss-120b", messages=messages, max_tokens=20)
choice = response.choices[0]
details = response.usage.completion_tokens_details
print(repr(choice.message.content))
print(choice.finish_reason)
print(response.usage.completion_tokens, details.reasoning_tokens)'' length 20 18
Reading the empty answer
- The content is an empty string, and no error was raised. The call succeeded and returned nothing to read.
- finish_reason is
length, the provider's way of saying the limit was reached before the model finished. - Most of the 20 completion tokens were reasoning tokens, shown in
reasoning_tokens. They were spent on thinking, and none were left for the answer.
The fix is a larger limit. The same call with 400 leaves room for both.
from litellm import completion
messages = [{"role": "user", "content": "Name one colour."}]
response = completion(model="groq/openai/gpt-oss-120b", messages=messages, max_tokens=400)
choice = response.choices[0]
details = response.usage.completion_tokens_details
print(repr(choice.message.content))
print(choice.finish_reason)
print(response.usage.completion_tokens, details.reasoning_tokens)'Blue.' stop 32 21
The answer is there and finish_reason is stop. The thinking still used some of the tokens, and the rest were the answer. Every example in this course uses a limit of at least 60, and most use 400, for this reason. Even 60 can be too small when a model thinks for a long time about a prompt, so a generous limit is the safe choice. A limit is only a ceiling. A large one costs nothing extra, because the model stops when it is done.
Calling two models at the same time with acompletion
acompletion() takes the same arguments as completion() and is awaited. It is the form to use inside async code, such as a web server, because other work can run while a call waits for its provider. asyncio.gather starts several calls together.
import asyncio
from litellm import acompletion
async def ask(model):
messages = [{"role": "user", "content": "Name one colour."}]
response = await acompletion(model=model, messages=messages, max_tokens=400)
return model, response.choices[0].message.content
async def main():
results = await asyncio.gather(ask("groq/openai/gpt-oss-120b"), ask("groq/openai/gpt-oss-20b"))
for model, answer in results:
print(model, "->", answer)
asyncio.run(main())groq/openai/gpt-oss-120b -> Blue. groq/openai/gpt-oss-20b -> Blue
Attribute access vs dictionary access
| Attribute style | Dictionary style | |
|---|---|---|
| The answer text | response.choices[0].message.content | response["choices"][0]["message"]["content"] |
| The token total | response.usage.total_tokens | response["usage"]["total_tokens"] |
| Use it when | Writing new code | Moving code written against OpenAI's JSON |
Where you use completion
- A script that sorts or summarises one piece of text with one call and one printed field.
- A web endpoint that awaits
acompletionso one slow provider does not block the other users. - A test or notebook that checks
finish_reasonto catch a reply that was cut off.
finish_reason when an answer is blank: length means the limit, not the prompt, is the problem.Related
- Previous: Installation and setup
- Next: Providers and keys: the model string
- Reference: Input params and Output format in the LiteLLM docs
- Set
max_tokens=60in the last example and run it several times. Note whether the answer is ever empty. - Add
"gemini/gemini-2.5-flash-lite"as a third model in theacompletionexample. - Print
response.choices[0].messagein full and read the fields it carries.
Every expert started right here.