
As I started exploring AI models and their APIs such as OpenAI, Claude, and others. I came across LangChain. LangChain is an open-source framework that provides pre-built integrations with most popular AI models, making it easier to work with them through a unified interface.
Initial Setup and Basic Usage
To start exploring LangChain, I used a simple example.
LangChain provides the init_chat_model utility, which allows us to connect to a chat model easily. It also supports parameters such as temperature, max_tokens, and more. For detailed options, refer to the
init chat model documentation.
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage, AIMessage, SystemMessage
model = init_chat_model("gpt-3.5-turbo", max_tokens=10)
Invoking the Model with Messages
We can invoke the model using prompts with different roles such as system, user, and assistant.
I’ve already written a separate blog, “Understanding Instruction Authority for LLMs,” where I explain system and user roles in more detail. You can read it here
messages = [
{"role": "system", "content": "You are a poetry expert"},
{"role": "user", "content": "Describe India"},
]
response = model.invoke(messages)
print(response.content)
Streaming Responses
LangChain also supports streaming outputs, which is useful for real-time responses.
for chunk in model.stream(messages):
print(chunk.text, end="|", flush=True)
Batch Invocation
We can invoke the model in batch mode to process multiple prompts at once.
responses = model.batch([
"Why do parrots have colorful feathers?",
"How do airplanes fly?",
"What is quantum computing?"
])
for response in responses:
print(response)
Tool Calling (Function Calling)
One of the most powerful features of LangChain is tool calling (also known as function calling).
In scenarios where we need to:
Fetch data from a database
Call an external API
Retrieve information from documents
we can define that logic inside a function (tool) and bind it to the model using bind_tools.
The model is intelligent enough to decide when to call a tool based on user intent.
In the code below, we define a weather tool and bind it to the model. Once the tool is bound, the model will automatically decide when to call it based on the user’s intent.
from langchain.tools import tool
@tool
def get_weather(location: str) -> str:
"""Get the weather at a location."""
return f"It's sunny in {location}."
model = init_chat_model("gpt-3.5-turbo", max_tokens=100)
model_with_tools = model.bind_tools([get_weather])
response = model_with_tools.invoke("Is it raining in India?")
print(response)
for tool_call in response.tool_calls:
# View tool calls made by the model
print(f"Tool: {tool_call['name']}")
print(f"Args: {tool_call['args']}")
response = model_with_tools.invoke("Is it raining in India?")
For this prompt, the user is clearly asking about weather conditions, so the model recognises the intent and calls the weather tool to fetch the required information.
response = model_with_tools.invoke("Where is India?")
In this case, the user is asking a general knowledge question, not related to weather. The model understands that calling the weather tool is unnecessary, so the tool is not invoked, and the model responds directly.
You can inspect the tool calls made by the model:
for tool_call in response.tool_calls:
print(f"Tool: {tool_call['name']}")
print(f"Args: {tool_call['args']}")
Forcing Tool Usage
If you want the model to always call a tool, regardless of user intent, you can explicitly specify it using the tool_choice parameter.
model_with_tools = model.bind_tools([tool_1], tool_choice="any")
This forces the model to invoke a tool on every request.
Learn more about Tool Calling
For a deeper understanding of Tool (Function) Calling, including advanced patterns, error handling, and real-world use cases, you can refer to the official LangChain documentation.
Final Thought
Learning AI models and their APIs can feel overwhelming at first, especially with the amount of new terminology and rapidly evolving technologies. What helped me most was starting simple reading the documentation, experimenting with small examples, and understanding the fundamentals before moving on to more complex concepts.
LangChain, in particular, makes it easier to experiment across different models, handle prompts, stream responses, and integrate tools based on user intent, all without being tied to a single provider.
As I continue exploring, my focus remains the same: build small, experiment often, and strengthen foundational understanding.



