LiteLLMLiteLLM 1.105 · Python 3.10+
Dashboard
0%

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.

python
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.

python
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.

ExampleAPI keyA system message and a user message
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)

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.

ExampleAPI keyThe fields of a ModelResponse
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 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 stop when 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_tokens includes any hidden thinking tokens.
  • The last line is True because 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.

ExampleAPI keyA reply limit of 20 on a reasoning model
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)

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.

ExampleAPI keyThe same call with a limit of 400
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)

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.

ExampleAPI keyTwo models called 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())

Attribute access vs dictionary access

Attribute styleDictionary style
The answer textresponse.choices[0].message.contentresponse["choices"][0]["message"]["content"]
The token totalresponse.usage.total_tokensresponse["usage"]["total_tokens"]
Use it whenWriting new codeMoving 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 acompletion so one slow provider does not block the other users.
  • A test or notebook that checks finish_reason to catch a reply that was cut off.
Watch out. An empty answer with no exception is almost always a reply limit that is too low on a reasoning model. Print finish_reason when an answer is blank: length means the limit, not the prompt, is the problem.
Try it yourself
  • Set max_tokens=60 in 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 the acompletion example.
  • Print response.choices[0].message in full and read the fields it carries.

Every expert started right here.