LLM-Based AI Agent Development
An AI agent based on LLM is a system where a language model acts as the "brain", making decisions: what to do next, which tools to use, how to interpret results. Unlike a RAG system or chatbot, an agent doesn't just generate an answer — it executes a sequence of actions to achieve a goal.
AI Agent Components
┌─────────────────────────────────────────┐
│ LLM (reasoning) │
│ ┌─────────┐ ┌──────────┐ ┌────────┐ │
│ │Planning │ │ Tool │ │Memory │ │
│ │ │ │ Calling │ │ │ │
│ └─────────┘ └──────────┘ └────────┘ │
└─────────────────────────────────────────┘
↓ ↓
┌─────────┐ ┌──────────────┐
│ Actions │ │ Tools │
└─────────┘ ├──────────────┤
│ Web Search │
│ Code Exec │
│ DB Query │
│ API Calls │
│ File I/O │
└──────────────┘
ReAct Pattern: Foundation of Most Agents
ReAct (Reasoning + Acting) is a pattern where the model alternates between reasoning and actions:
Thought: Need to find current USD/RUB exchange rate
Action: search_web("USD exchange rate central bank today")
Observation: According to central bank data on 28.03.2026: 1 USD = 89.43 RUB
Thought: Found the rate. Now I can calculate the amount
Action: calculate(amount_usd=1500 * 89.43)
Observation: 134145.0
Thought: Calculation complete. Final answer: 134,145 rubles
Final Answer: At 89.43 rubles/dollar, 1500 USD = 134,145 RUB.
Basic Agent Implementation with OpenAI Tools
from openai import OpenAI
import json
client = OpenAI()
# Define tools
tools = [
{
"type": "function",
"function": {
"name": "search_documents",
"description": "Search information in corporate knowledge base",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"},
"doc_type": {"type": "string", "enum": ["contract", "policy", "faq"]},
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "execute_sql",
"description": "Execute SQL query to company database",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "SELECT-only SQL query"},
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "send_email",
"description": "Send email",
"parameters": {
"type": "object",
"properties": {
"to": {"type": "string"},
"subject": {"type": "string"},
"body": {"type": "string"},
},
"required": ["to", "subject", "body"],
},
},
},
]
# Tool handlers
def execute_tool(tool_name: str, tool_args: dict) -> str:
if tool_name == "search_documents":
results = vectorstore.similarity_search(tool_args["query"], k=3)
return "\n".join([r.page_content for r in results])
elif tool_name == "execute_sql":
results = db.execute(tool_args["query"])
return str(results[:20]) # Limit output
elif tool_name == "send_email":
email_service.send(**tool_args)
return "Email sent successfully"
return "Tool not found"
# Agent loop
def run_agent(user_message: str, max_iterations: int = 10) -> str:
messages = [
{"role": "system", "content": "You are a corporate AI assistant. Use tools to complete tasks."},
{"role": "user", "content": user_message},
]
for iteration in range(max_iterations):
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
tool_choice="auto",
)
message = response.choices[0].message
# If no tool calls — return final answer
if not message.tool_calls:
return message.content
# Process tool calls
messages.append(message)
for tool_call in message.tool_calls:
tool_name = tool_call.function.name
tool_args = json.loads(tool_call.function.arguments)
result = execute_tool(tool_name, tool_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
})
return "Maximum iterations exceeded"
Practical Case: Agent for Purchase Request Processing
Task: Agent receives purchase request from employee, checks budget, finds suitable suppliers in registry, creates contract draft, sends for approval.
Agent tools:
-
check_budget(department, category)— check budget balance -
search_suppliers(category, requirements)— find suppliers -
generate_contract_draft(supplier_id, terms)— generate contract -
create_task_in_jira(title, description, assignee)— create task -
send_notification(user_id, message)— send notification
Metrics over 3 months:
- Request processing time: 4.5 days → 2.1 hours
- Automatically processed (no edits): 68%
- Errors (wrong supplier/budget exceeded): 4%
Guardrails: Agent doesn't execute financial operations directly — only document preparation and notifications. Final approval by human.
Memory: Short-term and Long-term
from langchain.memory import ConversationBufferWindowMemory, ConversationSummaryMemory
# Short-term memory: last N messages
short_term = ConversationBufferWindowMemory(k=10, return_messages=True)
# Long-term: summarized history
long_term = ConversationSummaryMemory(llm=ChatOpenAI(), max_token_limit=2000)
# Semantic memory: important facts in vector store
from langchain.memory import VectorStoreRetrieverMemory
semantic_memory = VectorStoreRetrieverMemory(
retriever=vectorstore.as_retriever(search_kwargs={"k": 3})
)
Timeline
- Basic agent with 3–5 tools: 2–3 weeks
- Enterprise agent with integrations: 6–10 weeks







