await agent.invoke({
messages: [
{
role: 'human',
content: 'What is the weather in Kochi, Kerala? Explain it like a story.'
}
]
});
Execution Flow
Loading diagram…
Why This Is Powerful
Notice that the tool only returns raw weather information.
The LLM is responsible for transforming that data into a natural language response.
The same tool can support summaries, stories, reports, and recommendations.
Extending Your Agent Further
Using a System Prompt
You can provide a system prompt directly when creating an agent. This allows you to control the agent's behavior, tone, and decision-making process.
const agent = createAgent({
model,
tools: [weatherTool],
systemPrompt: `
You are a helpful weather assistant.
Always explain the forecast in simple language.
`,
});
Building a Search Agent
A search agent follows almost the same architecture as the weather agent.
Instead of connecting a weather API through a tool, you connect a browser or web-search tool. The agent can then decide when to search the web and use the returned information to answer the user.
Loading diagram…
The overall design remains the same. You simply swap the weather tool for a browser tool.
A simple implementation using DuckDuckGo looks like this:
import { createAgent, tool } from 'langchain';
import * as z from 'zod';
import 'dotenv/config';
const searchTool = tool(
async ({ input }) => {
const url = new URL(`https://api.duckduckgo.com/?q=${encodeURIComponent(input)}&format=json`);
const res = await fetch(url);
if (!res.ok) {
const body = await res.text();
throw new Error(`Search API failed (${res.status}): ${body}`);
}
const data = await res.json();
const results = data;
return results ?? 'No results found';
},
{
name: 'search',
description: 'Search the web for information',
schema: z.object({
input: z.string().describe('The input to search for'),
}),
}
);
const agent = createAgent({
model: 'gpt-5.4',
tools: [searchTool]
});
console.log(
await agent.invoke({
messages: [
{ role: 'human', content: 'When is FIFA starting?'}
]
})
);
With this approach, the model can decide when to call the search tool, retrieve current information from the web, and then generate a natural-language answer.
Giving Your Agent a Name
Agents can also be assigned a name using the name parameter.
This becomes particularly useful when working with LangGraph or multi-agent systems because named agents are easier to identify in graph visualizations, traces, and debugging sessions.