AI Agent Framework
Relevant source files
The following files were used as context for generating this wiki page:
- packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/agent.service.ts
- packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-execution.service.ts
- packages/twenty-front/src/pages/settings/ai/SettingsAI.tsx
- packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/ai-agent-action/components/WorkflowEditActionAiAgent.tsx
- packages/twenty-server/src/engine/metadata-modules/ai/ai-agent/constants/agent-system-prompts.const.ts
- packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/standard-agents/agents/metadata-builder-agent.ts
- packages/twenty-server/src/engine/workspace-manager/workspace-sync-metadata/standard-agents/agents/workflow-builder-agent.ts
- packages/twenty-apps/community/meeting-transcript/serverlessFunctions/process-transcript/src/index.ts
The AI Agent Framework in Twenty is a sophisticated system designed to interpret user requests and execute complex tasks by leveraging specialized, tool-equipped AI agents. It acts as an intelligent layer that can modify the data model, perform CRUD operations on records, build automation workflows, and more. The framework is built around a central router that dispatches tasks to the appropriate agent, an execution engine that manages the interaction with AI models, and a dynamic tool generation system that grants agents the ability to interact with the Twenty CRM environment.
This framework is integral to features like AI-powered chat, automated meeting transcript processing, and intelligent workflow actions. It provides a modular and extensible way to add new AI capabilities to the platform.
Architecture Overview
The framework consists of several key backend components that work together to process a user's request and produce a result. The flow begins with user input, which is analyzed by a router. The router then decides on an execution strategy, either dispatching the task to a single agent ("simple" strategy) or creating a multi-step plan for multiple agents ("planned" strategy). The AgentExecutionService then manages the entire process, from building context and generating tools to streaming the final response.
This diagram illustrates the high-level architecture of the AI Agent Framework.
Core Components
AI Agents
An AI Agent is a specialized entity defined by a prompt, a set of capabilities (tools), and a configuration. Each agent is designed to handle a specific domain of tasks within the Twenty ecosystem. Standard agents are defined in the system and are synchronized into a workspace.
An agent is defined by the StandardAgentDefinition interface, which includes the following key properties:
| Property | Type | Description |
|---|---|---|
standardId | string | A unique identifier for the standard agent. |
name | string | The internal, camelCase name of the agent (e.g., metadata-builder). |
label | string | The user-facing display name (e.g., "Metadata Builder"). |
description | string | A brief explanation of the agent's purpose and capabilities. |
icon | string | The name of the UI icon for the agent. |
prompt | string | The system prompt that instructs the AI on its role, capabilities, and constraints. |
modelId | string | The ID of the AI model to be used for this agent. |
standardRoleId | string | The ID of the role that defines the agent's permissions. |
Sources: metadata-builder-agent.ts:6-11, workflow-builder-agent.ts:5-11
Standard Agents
The framework includes several standard agents, each with a distinct responsibility. The AI Router is responsible for selecting the correct agent based on the user's prompt.
| Agent Name | Label | Description |
|---|---|---|
metadata-builder | Metadata Builder | Modifies the data model/schema (e.g., creating objects, adding fields). |
data-manipulator | Data Manipulator | Performs CRUD operations on data records (e.g., creating a company, finding a person). |
helper | Helper | Answers questions about how to use Twenty, its features, and setup. |
researcher | Researcher | Finds external information from the web. |
workflow-builder | Workflow Builder | Creates and manages automation workflows. |
dashboard-builder | Dashboard Builder | Creates and manages dashboards and visualizations. |
| Sources: agent-system-prompts.const.ts:20-31, metadata-builder-agent.ts, workflow-builder-agent.ts |
AI Router
The AI Router is the entry point for handling user messages. It analyzes the user's request and determines the most effective strategy for fulfilling it. It uses a specialized system prompt to make this decision.
The router chooses between two main strategies:
- Simple Strategy: Used when a single agent can handle the entire request. The router identifies the appropriate agent and may provide
toolHints(e.g., relevant objects and operations) to guide the agent. - Planned Strategy: Used for complex requests that require the coordination of multiple agents. The router generates a multi-step plan, where each step is assigned to a specific agent and may depend on the output of previous steps.
The following diagram shows the decision-making process of the AI Router.
Sources: agent-system-prompts.const.ts:14-86
A critical function of the router is to distinguish between requests that modify the database schema versus those that modify data records. For example:
"Create an object called Project"is routed tometadata-builder."Create a company called Acme"is routed todata-manipulator.
Sources: agent-system-prompts.const.ts:33-38
System Prompts
System prompts are crucial for guiding the behavior of the AI models. The framework defines a set of constant prompts for different contexts.
| Prompt Name | Purpose |
|---|---|
BASE | Provides general instructions on tool usage, error recovery, and permissions. |
CHAT_ADDITIONS | Instructs the agent to format responses with markdown for better readability in a chat context. |
WORKFLOW_ADDITIONS | Provides context that the agent is running as part of a workflow and its output may be used by subsequent steps. |
ROUTER | A detailed prompt for the AI Router to help it decide between "simple" and "planned" strategies and select the correct agent. |
OUTPUT_GENERATOR | Instructs an agent to convert execution results into a structured format based on a given schema, often used in workflows. |
| Sources: agent-system-prompts.const.ts:1-100 |
Agent Execution Service
The AgentExecutionService is the backend service responsible for orchestrating the execution of an AI agent. Its primary method, streamChatResponse, handles the end-to-end process of receiving a request, preparing the AI call, and streaming the response back to the client.
The execution flow is as follows:
- Find Agent: Retrieves the specified agent's definition.
- Build Context: If
recordIdsare provided, it fetches the corresponding data from the database to create aCONTEXTstring. This is handled bygetContextForSystemPrompt. - Prepare AI Request: The
prepareAIRequestConfigmethod assembles the final configuration for the AI model call. This includes:- Combining the base system prompt, agent-specific prompt, and the dynamic context string.
- Generating tools for the agent using
AgentToolGeneratorServicebased on the agent's role and permissions. - Converting messages to the format required by the AI model.
- Stream Response: It calls the AI provider using
streamTextfrom theailibrary and streams the response. - Billing: It calculates and bills the AI usage for the workspace.
The following sequence diagram shows the process within streamChatResponse.
Sources: agent-execution.service.ts:285-408
Frontend & App Integration
The AI Agent Framework is exposed to users in several parts of the Twenty application.
AI Settings
The AI settings page allows users to manage their AI configurations. While the provided source file is primarily a UI placeholder, it indicates a dedicated area for AI-related settings within the application.
Sources: SettingsAI.tsx
Workflow Actions
AI Agents can be integrated directly into workflows as an action step. The WorkflowEditActionAiAgent component provides the UI for this. Users can:
- Select an AI agent from a list.
- Provide a prompt for the agent to execute.
- Define an output schema for the agent's response, allowing the structured output to be used in subsequent workflow steps.
This powerful feature enables complex, AI-driven automations, such as analyzing incoming data, generating content, or making decisions based on CRM records.
Sources: WorkflowEditActionAiAgent.tsx
Serverless Functions (Community Apps)
The AI capabilities are also used in community-developed applications. For example, the "Meeting Transcript" app uses an AI model to analyze meeting transcripts, extract summaries, key points, and action items, and then create corresponding notes and tasks in Twenty. This demonstrates how the underlying AI infrastructure can be leveraged by serverless functions to create powerful integrations.
The analyzeTranscript function in this app uses an OpenAI-compatible API to process text and return structured JSON, which is then used to create records in the CRM via the REST and GraphQL APIs.
Sources: process-transcript/src/index.ts:555-668
Summary
The AI Agent Framework is a cornerstone of Twenty's intelligent features. By combining specialized agents, a smart router, dynamic tool generation, and contextual data, it provides a flexible and powerful system for automating tasks and interacting with the CRM in natural language. Its integration into workflows and its use in community apps highlight its importance as a platform-level capability, enabling a wide range of AI-powered functionalities.