Connect to any LLMs
CrewAI can talk to many LLM providers (cloud and local) through one unified LLM class. You choose the model you want, configure its keys once, and then plug that LLM into agents, crews, or flows.
1. LLM Basics for Agentic Architects
- LLMs are the “brain” of your agents: they read context, decide what to do, and generate text.
- Context window = how much text fits in one call (e.g., 8K vs 128K vs 1M tokens).
- Temperature (0.0–1.0) controls randomness: low for deterministic tools, higher for creative writing.
- Provider = where the model runs (OpenAI, Anthropic, Google, local Ollama, etc.).
As an architect, you pick which brain and how it should think (temperature, max tokens, etc.).
2. The Core LLM Class
Most providers follow this basic pattern:
You can then:
- Pass llm= into an Agent, Crew, or Flow.
- Or call it directly:
CrewAI also supports:
- stream=True for streaming tokens.
- await llm.acall(...) for async calls.
- response_format=MyPydanticModel for structured outputs.
3. Fastest Setup: Use MODEL= in .env
For quick scaffolds (especially with crewai create):
- CrewAI will default to that model whenever you don’t pass a custom LLM.
- For production, it’s still good practice to be explicit with LLM(...) on critical agents.
4. Popular Cloud Providers (Cheat Sheet)
Below is a didactical “menu” of the most common providers and how to wire each one.
4.1 OpenAI (GPT‑4.x, GPT‑4o, o‑series)
Env vars:
Basic LLM:
Advanced (Responses API, reasoning, streaming, etc.) are also supported (e.g., api="responses", reasoning_effort="medium").
When to use: general‑purpose, best‑in‑class, strong tools + structured outputs.
4.2 Anthropic (Claude 3 / 4 family)
Env vars:
Basic:
Extended Thinking (for Claude Sonnet 4+):
When to use: deep reasoning, agentic workflows, and long‑context analysis.
4.3 Google Gemini (Gemini API + Vertex)
Env vars (Gemini API):
Basic Gemini API usage:
Vertex AI Express mode (API key):
Full Vertex with service account uses project, location, and vertex_credentials JSON.
When to use: multimodal work, Google Cloud integration, very large context windows.
4.4 Azure OpenAI / Azure AI Inference
Env vars:
Code:
When to use: enterprise compliance, VNet, data residency.
4.5 AWS Bedrock
Env vars:
Code:
Supports Anthropic, Llama, Nova, Mistral, etc. with Guardrails and model‑specific params.
4.6 Other major providers (same pattern)
All below use the same idea: set an API key env var, then call LLM(model="provider/model-id", ...).
- Groq – GROQ_API_KEY, models like "groq/llama-3.2-90b-text-preview".
- Nvidia NIM (cloud) – NVIDIA_API_KEY, models under nvidia_nim/....
- IBM watsonx.ai – WATSONX_URL, WATSONX_APIKEY, WATSONX_PROJECT_ID.
- Hugging Face Hub – HF_TOKEN, model="huggingface/meta-llama/Meta-Llama-3.1-8B-Instruct".
- Perplexity, Fireworks, SambaNova, Cerebras, OpenRouter, Nebius, etc.
The docs list model IDs, context windows, and best‑for columns to help you choose.
5. Local and Self‑Hosted Models
For privacy, latency, or cost reasons you can also run models yourself.
5.1 Ollama (local LLMs)
Steps:
- Install Ollama and pull a model:
- Point CrewAI at it:
5.2 Local Nvidia NIM (OpenAI‑compatible)
If you expose NIM as an OpenAI‑compatible endpoint:
Then reuse this llm in your agents (e.g., via @CrewBase).
6. Streaming, Async, and Structured Responses
6.1 Streaming
To stream tokens as they’re generated:
Useful for UIs and long‑running tasks (and is also supported at the crew/flow level).
6.2 Async calls
For concurrent LLM calls
acall accepts the same parameters as call (messages, tools, callbacks, etc.).
6.3 Structured outputs (Pydantic)
You can ask the LLM to return typed objects instead of raw JSON/text:
This is powerful for building reliable agent outputs (e.g., plans, tool configs, JSON APIs).
7. Design Tips for “Connect Any LLM” Lessons
When architecting your solution, consider these patterns:
-
Separate what the agent does (role, tools, knowledge) from which brain it uses (LLM(model=...)).
-
Start learners on one provider (e.g., openai/gpt-4o), then swap to Groq, Gemini, or Ollama by changing only:
- env variables and
- the model= string.
-
Use:
- Low temperature for tools, planning, evaluations.
- Higher temperature for ideation, copy, creative work.
-
Show streaming + async for UX and throughput, and structured outputs for robust integrations.
This framing makes “Connect to Any LLM” a matter of configuration, not code rewrites, which is exactly the mindset you want for Agentic Architects.