π§ The Hidden Power of AI Agents in Web Apps: Build a Truly Smart App in Less than 100 Lines of Code
Imagine your web app actually thinks. Not just a chatbot. Not just GPT copy-paste. A decision-making, task-managing, user-adapting digital assistant. Welcome to the world of AI agents inside web apps β and yes, weβre doing it in under 100 lines of code.
Unlike a simple chatbot or LLM wrapper (like GPT in a textarea), an AI Agent has:
Think of ChatGPT on steroids β mixed with Jarvis from Iron Man.
If your app depends on communicating with users, managing tasks, or transforming data β you can basically automate it with an agent. Customer onboarding, knowledge base assistants, form automation, internal dashboards, project organizers... all can have autonomous intelligence now.
We'll build a simple AI Agent in a web app with:
No doomscrolling required. β Let's go.
A micro-agent that:
Hereβs what it might do:
"Remind me to drink water every 3 hours, and give me todayβs weather in Berlin."
npm init -y npm install express langchain openai dotenv
Create a .env file:
OPENAI_API_KEY=your-super-secret-api-key
This is our AI brain in about 40 lines.
const { initializeAgentExecutorWithOptions } = require("langchain/agents");
const { OpenAI } = require("langchain/llms/openai");
const { SerpAPI, Calculator } = require("langchain/tools");
require('dotenv').config();
const tools = [
new Calculator(),
new SerpAPI(), // You can mock this with custom APIs too
];
const model = new OpenAI({ temperature: 0, openAIApiKey: process.env.OPENAI_API_KEY });
async function runAgent(userInput) {
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: 'zero-shot-react-description',
});
console.log("π€ Agent initialized. Ask: ", userInput);
const result = await executor.call({ input: userInput });
return result.output;
}
module.exports = runAgent;
Minimal Express backend:
const express = require('express');
const runAgent = require('./agent');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
app.post('/agent', async (req, res) => {
const userInput = req.body.input;
const output = await runAgent(userInput);
res.json({ output });
});
app.listen(3001, () => console.log('π§ Agent server on http://localhost:3001'));
Simple frontend:
<!DOCTYPE html>
<html>
<head>
<title>Task Buddy</title>
</head>
<body>
<h1>π§ Task Buddy</h1>
<textarea id="input" placeholder="Tell me what to do..." rows="4" cols="50"></textarea><br>
<button onclick="runAgent()">Run Agent</button>
<pre id="output"></pre>
<script>
async function runAgent() {
const input = document.getElementById('input').value;
const res = await fetch('http://localhost:3001/agent', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ input })
});
const data = await res.json();
document.getElementById('output').textContent = data.output;
}
</script>
</body>
</html>
Ask:
"Summarize the news about AI stocks and calculate if I invested $500 in Nvidia last week at $X per share."
π₯ It fetches news, calculates stock performance and replies with recommendations. In 1 go.
Always validate inputs/outputs, especially if your agents get access to real data. Use schema validators like Zod.
You don't have to imagine the future β it's already here. AI Agents make any website think for itself. You can build powerful, autonomous micro-apps that solve user problems without manual logic trees.
The fact that this works in under 100 lines blows our minds. And itβs only getting better.
So, ask yourself π₯
If your app had a mind of its own, what would it do?
Then build it π§ π₯
Happy agent-building, future-coder π§ βοΈ
β‘οΈ If you need help building AI-powered agents or smart assistants in your web app β we offer such services!
Information