Building a Multi-Agent AI System from Scratch (No Frameworks)
The Concept
5 AI agents collaborate in a pipeline: Researcher โ Writer โ Editor โ Reviewer โ Publisher. Each agent has a role, a system prompt, and passes output to the next.
Implementation
class Agent:
def __init__(self, name, role, system_prompt):
self.name = name
self.role = role
self.system_prompt = system_prompt
def process(self, input_text):
"""Call LLM with system prompt + input"""
import requests
response = requests.post(
"http://localhost:11434/api/generate",
json={
"model": "llama3.2",
"prompt": f"{self.system_prompt}\n\nInput: {input_text}",
"stream": False
}
)
return response.json()["response"]
class Pipeline:
def __init__(self, agents):
self.agents = agents
def run(self, topic):
output = topic
for agent in self.agents:
print(f"๐ค {agent.name} working...")
output = agent.process(output)
print(f" โ
Done ({len(output)} chars)")
return output
# Define agents
agents = [
Agent("Researcher", "research", "Find 5 key facts about the topic. Be concise."),
Agent("Writer", "write", "Write a 500-word article from the research."),
Agent("Editor", "edit", "Improve clarity, fix grammar, make it engaging."),
Agent("Reviewer", "review", "Score 1-10. List what works and what needs fixing."),
Agent("Publisher", "publish", "Format as markdown with title, headings, and CTA."),
]
# Run
pipeline = Pipeline(agents)
result = pipeline.run("The Future of Local AI")
print(result)
Why No Framework?
- Simpler: 50 lines vs 500 lines with CrewAI
- Debuggable: You can see exactly what's happening
- Flexible: Add/remove agents easily
- No dependencies: Just
requestslibrary - Works with any LLM: Ollama, OpenAI, Groq, anything
Pro Tips
- Different models per agent: Use
codellamafor code review,llama3.2for writing - Temperature: Lower for Researcher (factual), higher for Writer (creative)
- Fallback: If one model fails, try another
- Logging: Save each agent's output for debugging
Use Cases
- Blog post pipeline
- Video script generation
- Social media content
- Research reports
- Code review chains
๐ Full implementation: github.com/amrendramishra/ai-tools
๐ amrendranmishra.dev
Building AI tools daily. Follow for more.
Comments
No comments yet. Start the discussion.