LangChain Mastery

Course Content

LangChain Mastery

7 sections · 109 lessons

How do you use LangChain to integrate with external APIs like OpenAI?


What you need to know

There are two kinds of "external API" in an LLM app, and they are integrated differently.

Model providers

  • OpenAI, Anthropic, Google, Mistral, local servers
  • Use the provider package's chat model
  • Same interface for all of them

Business APIs

  • CRM, payments, weather, your own services
  • Wrap with @tool or a function step
  • You own the HTTP client and error handling

Model providers

Python
from langchain_openai import ChatOpenAIllm = ChatOpenAI(model="gpt-5.4-mini", timeout=30, max_retries=2)  # reads OPENAI_API_KEY# Any OpenAI-compatible endpoint: vLLM, a gateway, a proxylocal = ChatOpenAI(model="meta-llama/Llama-3.1-8B-Instruct",                   base_url="http://localhost:8000/v1", api_key="EMPTY")

Azure OpenAI has its own class, AzureChatOpenAI, because it uses deployment names and API versions. Ollama has a dedicated package, langchain-ollama with ChatOllama. With init_chat_model you can pass model_provider="openai" and base_url to reach a compatible server in the same way.

Business APIs

Python
import httpxfrom langchain.tools import tool@tooldef get_listing_price(listing_id: str) -> str:    """Return the asking price in rupees for a property listing ID."""    r = httpx.get(f"https://api.example-realty.in/listings/{listing_id}", timeout=10)    r.raise_for_status()    return str(r.json()["price_inr"])

The docstring and type hints become the tool's description and input schema, which the model reads when deciding to call it. If the call is always needed, skip the tool and put a function in the chain; LangChain wraps it as a RunnableLambda.

Production basics

  • Keys come from the environment or a secret manager, never a literal in code.
  • Set timeout on both the model and your own HTTP client. A hung call without a timeout holds a worker until the load balancer kills it.
  • Return short, clean strings from tools. A 20 KB JSON dump wastes tokens and confuses the model.

A real-life example

A real-estate firm's lead-qualification bot talks to buyers on WhatsApp. The LLM runs through ChatOpenAI, but in staging the team points base_url at a self-hosted vLLM server to avoid paying for test traffic. The bot needs two business APIs: the listings service (price and availability) and the CRM (create a lead). Price lookup is a @tool, because the model decides whether the buyer asked about price. Creating the CRM lead is a fixed step at the end of every qualified conversation, so it is a plain function in the chain, not a tool. That split kept the agent from creating duplicate leads, which had happened 40 times in the first week when "create lead" was a tool the model could call whenever it liked.

Follow-up questions to expect

  • "How do you call an internal model behind a company gateway?" — If it speaks the OpenAI API, ChatOpenAI(base_url=..., api_key=...); otherwise write a small custom chat model class.
  • "Where do you handle the API's errors?" — Inside the tool: catch the HTTP error and return a clear message ("listing not found") so the model can recover, and log the details for yourself.
  • "Should every API be a tool?" — No. Only calls the model needs to choose. Fixed steps are cheaper and more predictable as plain chain steps.