What Is an AI Agent? From Large Language Models to Autonomous Agents
This article explains why large language models are powerful yet limited, defines AI agents as autonomous systems that combine a model, memory, tools, and actions, details the ReAct reasoning‑and‑acting loop, provides a 30‑line Python LangChain example and a Java Spring AI implementation, and outlines five practical use‑case scenarios and the roadmap for the series.
Large Models Are Powerful but Limited
GPT‑4, Claude 3 and similar models can answer questions, write articles, translate and generate code, but they cannot access real‑time data, call external services, retain information beyond their context window, act proactively or perform multi‑step operations without explicit guidance.
AI Agent Architecture
Agent is an autonomous entity that can think, use tools, execute tasks, and retain context.
The large model serves as the "brain"; the agent equips it with "hands" (tools) and "feet" (actions) and adds memory to preserve dialogue.
Four Core Components
Brain (LLM) : understands tasks, devises plans, decides the next step.
Memory : retains historical dialogue and contextual information.
Tools : call external APIs, execute code, manipulate files.
Action : performs concrete operations and observes results.
ReAct (Reason + Act) Workflow
┌─────────────────────────────────────────────────────────────────┐
│ ReAct Execution Flow │
├─────────────────────────────────────────────────────────────────┤
│ Thought: I need to check today's weather in Beijing │
│ ▼ │
│ Action: Call weather API with parameter city=Beijing │
│ ▼ │
│ Observation: Beijing today is sunny, 25°C │
│ ▼ │
│ Thought: The user wants to go out, I can remind them to bring │
│ sunscreen │
│ ▼ │
│ Action: Generate reply "Beijing's weather is nice today, bring │
│ sunscreen" │
│ ▼ │
│ Observation: Reply sent │
│ (Loop until the task is completed) │
└─────────────────────────────────────────────────────────────────┘Core idea: an agent repeatedly cycles through Thought → Action → Observation → Thought … until the overall goal is satisfied.
Simple Python Agent (≈30 Lines)
Install dependencies with pip install langchain langchain-openai, define two tools (weather lookup and arithmetic), configure the OpenAI model, provide a prompt template that enforces the ReAct format, create the agent and execute it.
# simple_agent.py
# Install dependencies: pip install langchain langchain-openai
from langchain.agents import AgentExecutor, create_react_agent
from langchain.tools import tool
from langchain_openai import ChatOpenAI
@tool
def get_weather(city: str) -> str:
"""Get weather for the specified city"""
weather_data = {
"Beijing": "Sunny, 25°C",
"Shanghai": "Cloudy, 22°C",
"Shenzhen": "Rainy, 28°C",
}
return weather_data.get(city, f"Weather information for {city} not found")
@tool
def calculate(expression: str) -> str:
"""Calculate a mathematical expression"""
try:
result = eval(expression)
return f"Result: {result}"
except Exception:
return "Expression error"
llm = ChatOpenAI(
model="gpt-4",
temperature=0,
api_key="your-api-key",
)
prompt = """You are an intelligent assistant that can use tools to complete tasks.
Available tools:
{tools}
Tool names:
{tool_names}
Answer format:
Thought: what I need to do
Action: tool name
Action Input: tool parameters
Observation: tool result
... (repeat)
Final Answer: final response
Start!
Question: {input}
{agent_scratchpad}"""
agent = create_react_agent(
llm=llm,
tools=[get_weather, calculate],
prompt=prompt,
)
agent_executor = AgentExecutor(
agent=agent,
tools=[get_weather, calculate],
verbose=True,
)
result = agent_executor.invoke({"input": "How's the weather in Beijing? Then calculate 123 * 456"})
print(result["output"])Execution Result
> Entering new AgentExecutor chain...
Thought: I need to first query Beijing's weather
Action: get_weather
Action Input: Beijing
Observation: Sunny, 25°C
Thought: Weather checked, now I need to calculate 123 * 456
Action: calculate
Action Input: 123 * 456
Observation: Result: 56088
Thought: Both tasks are done, I can give the final answer
Final Answer: Beijing today is sunny, 25°C. The result of 123 * 456 is 56088.
> Finished chain.Java Spring AI Quick Start
Add the OpenAI starter dependency:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0-M4</version>
</dependency>Configure the model in application.yml:
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
model: gpt-4Spring beans that assemble the agent:
@Configuration
public class AgentConfig {
@Bean
public ChatClient chatClient(ChatModel chatModel) {
return ChatClient.builder(chatModel).build();
}
@Bean
public ToolCallback[] tools() {
return new ToolCallback[]{
new ToolCallbacks.GetWeatherTool(),
new ToolCallbacks.CalculateTool()
};
}
@Bean
public ChatClient agentClient(ChatModel chatModel, ToolCallback[] tools) {
return ChatClient.builder(chatModel)
.defaultTools(tools)
.build();
}
}Weather‑tool implementation (simulated data):
@Component
public static class GetWeatherTool implements Tool {
@Override
public String getName() { return "getWeather"; }
@Override
public String getDescription() { return "Get weather for a specified city"; }
@Override
public BiFunction<Map<String, Object>, Map<String, Object>, Object> getCallback() {
return (request, context) -> {
String city = (String) request.get("city");
return switch (city) {
case "Beijing" -> "Sunny, 25°C";
case "Shanghai" -> "Cloudy, 22°C";
default -> "Weather information not found";
};
};
}
}REST controller that forwards user input to the agent:
@RestController
public class AgentController {
@Autowired
private ChatClient agentClient;
@PostMapping("/agent/chat")
public String chat(@RequestBody String userInput) {
return agentClient.prompt(userInput).call().content();
}
}Typical Agent Scenarios
Intelligent Customer Service : automatically answer user queries, query orders, handle returns (LLM + RAG + Order API).
Programming Assistant : write code, explain code, locate bugs (LLM + Code Index + Execution Environment).
Data Analysis : natural‑language data queries, auto‑generate charts (LLM + SQL + Visualization Library).
Automated Operations : analyze alerts, execute remediation scripts (LLM + Monitoring API + Operation Scripts).
Personal Assistant : manage schedules, send reminders, auto‑reply emails (LLM + Calendar API + Email API).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
