Chat & LLM Models
Choosing and configuring LangChain chat models: providers, common params, streaming, structured output, and cost-aware patterns.
LangChain — chat models
EXAMPLE
# ===== Imports =====
from langchain_openai import ChatOpenAI
from langchain_anthropic import ChatAnthropic
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from pydantic import BaseModel, Field
# ===== Initialise =====
gpt = ChatOpenAI(
model='gpt-4o-mini',
temperature=0,
max_tokens=512,
timeout=30,
max_retries=2,
)
claude = ChatAnthropic(model='claude-haiku-4-5', temperature=0)
gemini = ChatGoogleGenerativeAI(model='gemini-1.5-flash')
# ===== Basic call =====
msg = gpt.invoke([
SystemMessage('You are a careful editor.'),
HumanMessage('Rewrite: their are 5 cars in the lot'),
])
print(msg.content)
# 'There are 5 cars in the lot.'
# ===== Prompt template =====
prompt = ChatPromptTemplate.from_messages([
('system', 'You answer in JSON only. Schema: {schema}'),
('human', '{question}'),
])
chain = prompt | gpt | StrOutputParser()
print(chain.invoke({
'schema': '{ "answer": string, "confidence": number }',
'question': 'What is the capital of France?',
}))
# ===== Streaming =====
for chunk in gpt.stream('Tell me a 5-line poem about coffee.'):
print(chunk.content, end='', flush=True)
print()
# Async:
# async for chunk in gpt.astream('...'):
# ...
# ===== Structured output via Pydantic =====
class Answer(BaseModel):
answer: str = Field(..., description='A short answer.')
confidence: float = Field(..., ge=0, le=1)
structured = gpt.with_structured_output(Answer)
result: Answer = structured.invoke('Capital of Japan?')
print(result.answer, result.confidence)
# ===== Tool / function calling =====
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Return current weather for a city."""
return f'Sunny in {city}.'
bound = gpt.bind_tools([get_weather])
res = bound.invoke('What is the weather in Sydney?')
# res.tool_calls -> [{ 'name': 'get_weather', 'args': { 'city': 'Sydney' }, 'id': '...' }]
# ===== Token + cost tracking =====
from langchain_community.callbacks import get_openai_callback
with get_openai_callback() as cb:
gpt.invoke('Say hello.')
print(cb.total_tokens, cb.total_cost)
# ===== Caching =====
from langchain_community.cache import SQLiteCache
import langchain
langchain.llm_cache = SQLiteCache(database_path='.langchain.db')
# ===== Provider swap (the win of the abstraction) =====
def call_model(model, q: str) -> str:
return (ChatPromptTemplate.from_messages([('human', '{q}')]) | model | StrOutputParser()).invoke({'q': q})
print(call_model(gpt, 'pi to 5 dp'))
print(call_model(claude, 'pi to 5 dp'))
print(call_model(gemini, 'pi to 5 dp'))
# ===== Patterns to internalise =====
# - temperature=0 for anything you intend to evaluate; small numbers above 0 for creative tasks
# - Use with_structured_output for any non-prose response
# - Stream long completions to keep latency perceived low
# - Add a cache for repeated dev/test prompts; saves real money
# - Pin model versions in code; do not let 'latest' drift into prod
# ===== Pitfalls =====
# - No timeout -> stuck tasks wait forever
# - Forgetting max_tokens on a chatty model -> blow your budget
# - Parsing JSON from a non-JSON-mode response -> brittle; use with_structured_output
# - tool calls without an executor -> message has tool_calls, your code must run them
# - Different providers strip system messages differently; test each
Why it matters
Chat models are the new dependency — choose like one. Pin the model + temperature, parse with structured output, stream long answers, cache during dev, and keep a clean swap layer so the abstraction earns its keep when a cheaper model lands next month.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
from langchain_openai import ChatOpenAI from langchain_anthropic import ChatAnthropic from langchain_community.chat_models import ChatOllama chat = ChatOpenAI(model='gpt-4o-mini', temperature=0.2) local = ChatOllama(model='llama3')Try it Yourself »
Discussion
Loading…