Basic Chat Examples
July 16, 2026 · View on GitHub
Last updated: 2026-07-16 Audience: Beginner · Time to complete: 5 minutes
This page shows the simplest ways to chat with an AI model using LLM4Free. Every provider implements the OpenAI-compatible chat.completions.create(...) interface, so the code shape is identical across vendors.
Table of Contents
- Using the unified Client
- Simplest example (no API key)
- Different providers
- Customizing responses
- Saving and reusing conversations
- Using the unified Client (details)
Using the unified Client
Start here. The unified Client is the recommended way to chat — it mirrors the OpenAI SDK, picks a working provider for you, and auto-fails over when one is down. Use model="auto" to let it choose, or model="Provider/Model" to force a specific backend.
from llm4free.client import Client
# Let the client pick any working provider/model
client = Client(print_provider_info=True)
print(client.chat.completions.create(
model="auto",
messages=[{"role": "user", "content": "Tell me a fun fact about space."}],
).choices[0].message.content)
# Force a specific provider/model
print(client.chat.completions.create(
model="HeckAI/google/gemini-2.5-flash-preview",
messages=[{"role": "user", "content": "Hello!"}],
).choices[0].message.content)
print(client.chat.completions.last_provider) # which provider was used
Tip
model="auto" resolves a working provider/model for you; model="HeckAI/google/gemini-2.5-flash-preview" forces a specific backend. print_provider_info=True prints the chosen provider/model live. The raw-provider examples below are still valid, but the Client gives you auto-failover and model resolution for free.
Simplest example (no API key)
HeckAI is a free provider — no key required.
from llm4free.llm.heckai import HeckAI
client = HeckAI()
response = client.chat.completions.create(
model="google/gemini-2.5-flash-preview",
messages=[{"role": "user", "content": "What is artificial intelligence?"}],
)
print(response.choices[0].message.content)
Output:
Artificial intelligence (AI) refers to computer systems designed to perform
tasks that typically require human intelligence — learning, reasoning,
problem-solving, language understanding, and visual perception...
Different providers
HeckAI (free)
from llm4free.llm.heckai import HeckAI
client = HeckAI()
print(client.chat.completions.create(
model="google/gemini-2.5-flash-preview",
messages=[{"role": "user", "content": "Hello!"}],
).choices[0].message.content)
ArtingAI (free)
from llm4free.llm.artingai import ArtingAI
client = ArtingAI()
print(client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
).choices[0].message.content)
FreeAI (free)
from llm4free.llm.freeai import FreeAI
client = FreeAI()
print(client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
).choices[0].message.content)
Groq (API key)
from llm4free.llm.Auth.groq import Groq
client = Groq(api_key="your-groq-key")
print(client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": "Explain machine learning simply"}],
).choices[0].message.content)
DeepInfra (API key)
from llm4free.llm.Auth.deepinfra import DeepInfra
client = DeepInfra(api_key="your-deepinfra-key")
print(client.chat.completions.create(
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
messages=[{"role": "user", "content": "What is Python?"}],
).choices[0].message.content)
Note
The exact model strings accepted depend on the provider. Free providers map friendly names like "gpt-4o" to a backend model. Authenticated providers use the upstream model id (for example, Groq's "llama-3.3-70b-versatile" or DeepInfra's "meta-llama/Meta-Llama-3.1-8B-Instruct").
Customizing responses
Standard OpenAI parameters are supported: temperature, max_tokens, top_p, and tools.
from llm4free.llm.heckai import HeckAI
client = HeckAI()
response = client.chat.completions.create(
model="google/gemini-2.5-flash-preview",
messages=[
{"role": "system", "content": "You are a concise tutor."},
{"role": "user", "content": "Explain recursion in one sentence."},
],
temperature=0.3,
max_tokens=120,
)
print(response.choices[0].message.content)
Saving and reusing conversations
Pass the full message list each call to keep context:
from llm4free.llm.heckai import HeckAI
client = HeckAI()
messages = [{"role": "user", "content": "My name is Ada."}]
r1 = client.chat.completions.create(model="google/gemini-2.5-flash-preview", messages=messages)
print(r1.choices[0].message.content)
# Append the assistant reply, then ask a follow-up
messages.append({"role": "assistant", "content": r1.choices[0].message.content})
messages.append({"role": "user", "content": "What was my name?"})
r2 = client.chat.completions.create(model="google/gemini-2.5-flash-preview", messages=messages)
print(r2.choices[0].message.content)
Using the unified Client (details)
Forget provider names — let Client pick one and auto-fail over. Pass model="auto" for automatic selection, or model="Provider/Model" to force a specific backend.
from llm4free.client import Client
client = Client(print_provider_info=True)
print(client.chat.completions.create(
model="auto",
messages=[{"role": "user", "content": "Tell me a fun fact about space."}],
).choices[0].message.content)
# Force a specific provider/model
print(client.chat.completions.create(
model="HeckAI/google/gemini-2.5-flash-preview",
messages=[{"role": "user", "content": "Hello!"}],
).choices[0].message.content)
See client.md for the full reference and streaming-responses.md for streaming.