Skip to main content

On This Page

Scalable AI Agent Architecture: Implementing a Modular Folder Structure in TypeScript

3 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Stop Messy AI Projects: A Clean Folder Structure for Real Agent Systems

Engineer Raju Dandigam identifies a critical failure point in AI development where projects degrade from simple index.ts scripts into unmanageable collections of files. He introduces a rigorous folder structure designed to scale as agents move from simple tool calls to complex, multi-step workflows. This system enforces a clear boundary where the model proposes actions while the application owns validation and safety.

Why This Matters

In traditional software, execution follows a deterministic path defined by the developer, but AI agent systems are inherently dynamic. Because the model’s decisions dictate the runtime flow, a lack of architectural structure makes these systems nearly impossible to debug or trace in production environments.

Adopting a modular structure is a technical necessity for safety. By isolating memory, prompts, and tools into distinct layers, developers can implement middleware for token budgets and rate limiting, preventing the non-deterministic nature of LLMs from causing cascading system failures or unexpected costs.

Key Insights

  • Traceable Execution: Folder structures in AI reflect architecture, ensuring behavior remains predictable even when the model determines the execution path (Dandigam, 2026).
  • Separation of Concerns: Moving text prompts into a dedicated /prompts directory allows for rapid iteration without modifying core application logic.
  • Controlled Tooling: Tools must be explicitly registered and validated within a /tools boundary to prevent the agent from gaining unauthorized system access.
  • Layered Memory: Systems should utilize a dedicated /memory folder to manage context intentionally, starting with simple message arrays before scaling to vector search.
  • Model Context Protocol (MCP): The /mcp directory serves as a clean boundary for external integrations, maintaining application control over access and permissions.

Working Examples

Recommended high-level folder structure for AI agent projects.

my-ai-agent/
├── src/
│ ├── agents/
│ ├── tools/
│ ├── memory/
│ ├── workflows/
│ ├── mcp/
│ ├── prompts/
│ ├── middleware/
│ ├── types/
│ └── index.ts

Example agent definition within the /agents directory.

export const researcherAgent = {
  name: "researcher",
  systemPrompt: "You are a research assistant...",
  tools: ["web_search"],
  temperature: 0.3,
};

Explicit tool registration and execution logic.

export const searchTool = {
  name: "web_search",
  execute: async (query: string) => {
    return fetch(`/search?q=${query}`);
  },
};

Coordinating multiple agent actions within the /workflows layer.

export async function researchPipeline(topic: string) {
  const research = await researcherAgent.run(topic);
  const analysis = await analystAgent.run(research);
  return analysis;
}

Practical Applications

  • System Role Definition: Use the /agents folder to define specific roles like ‘researcher’ or ‘analyst’ with unique system prompts and tool access. Pitfall: Hardcoding prompts in the main execution logic leads to rigid, untestable agents.
  • Operational Safety: Implement token tracking and rate limiting in the /middleware folder to manage production costs. Pitfall: Failing to isolate middleware allows agent loops to consume budgets without any safety interruption.
  • Context Management: Utilize a dedicated ContextMemory class in the /memory folder to manage message history. Pitfall: Pushing all history directly into prompts leads to context window overflow and degraded model performance.

References:

Continue reading

Next article

Resolving the Supabase Dual-DB Conflict in Lovable AI Workflows

Related Content