Skip to main content

Objective

An AI Agent system provides intelligent prospect qualification and lead generation through a multi-step, orchestrated workflow using multiple specialized agents. This architecture demonstrates how to build a comprehensive lead generation pipeline where different AI agents handle specific stages: conversation, qualification, and CRM insertion. Built on the modern Agents API framework with the A2A Protocol, each agent is independently configured with its own role, instructions, tools, and memory. Conversation context is maintained across turns using previous_response_id.

Use Case

AI Agent is ideal for applications that need:
  • Multi-Stage Lead Processing: Break down prospect qualification into specialized steps handled by different agents
  • Automated Prospect Qualification: Systematically collect prospect information and score leads based on qualification criteria
  • CRM Integration: Automatically insert qualified leads directly into your sales pipeline
  • Intelligent Routing: Route prospects through different workflows based on their responses and qualification level
  • Sales Process Automation: Reduce manual lead processing while maintaining high-quality prospect data

Prerequisite

Before configuring an AI Agent, ensure you have:
  • Orq.ai Account: Active workspace in the AI Studio.
  • API Access: Valid API key from Workspace Settings > API Keys.
  • Model Access: At least one text generation model enabled in the AI Gateway, such as gpt-4, claude-sonnet-4-6, or gpt-5-mini.
  • CRM API: Access to your CRM system API (Salesforce, HubSpot, Pipedrive, etc.) with authentication credentials for inserting new leads.

Multi-Agent Workflow Architecture

Workflow diagram showing Prospect Responses flowing through an Ingestion agent to a Qualification agent, which branches to Insertion (Insert to CRM) and scoring steps. Three Specialized Agents (Created via Agents API):
These agents are created using the modern Agents API framework with support for memory stores, knowledge bases, streaming, and the A2A Protocol for standardized communication.

Creating a CRM Insert Tool

First, create a tool for adding prospects to your CRM system. Head to the AI Studio:
  • Choose a Project and Folder and select the button.
  • Choose Tool.
  • Create a crminsert tool for adding prospects to your CRM

CRM Insert Tool Configuration

Configure the CRM tool with JSON schema for function calling:
The tool configuration defines the function signature that the AI model will call. Your code needs to handle the actual execution of this function and make the appropriate CRM API calls when the tool is invoked.
The JSON schema above defines the function signature. Tool execution requires a two-step workflow:Step 1: Receive tool calls - Agent returns with tool_calls array and enters “Input Required” state, pausing executionStep 2: Execute and resume - Your code executes the tool, then calls the agent again with the same previous_response_id and tool result to let the agent continueThis allows your code to integrate with external systems (CRM APIs, webhooks, databases) and feed results back to the agent for completion.

Handling Tool Calls

When an agent needs to use a tool (like inserting a prospect into CRM), it pauses and returns a response with tool_calls. Your code must execute the tool and send the result back via the continuation API:
Complete Tool Workflow:
  1. Agent calls tool: Agent decides it needs a tool and returns with tool_calls array
  2. Your code executes: Parse arguments and execute the actual business logic (CRM API call, database insert, external webhook, etc.)
  3. Agent resumes: Call the agent again with the same previous_response_id and pass the result back as input
  4. Agent completes: With the tool result, the agent continues processing and provides its final response
Key Implementation Details:
  • Tool arguments may be a JSON string or object - handle both with isinstance() (Python) or typeof checks (TypeScript)
  • Always use the response id as previous_response_id when continuing the conversation after tool execution
  • Send tool results back as a user message to resume the agent
  • The agent maintains full context - it remembers the tool call and your provided result

Creating Three Specialized Agents

Create three specialized agents using the Agents API. You can create agents via the Studio UI or programmatically via API.

1. Create the Ingestion Agent

You can create the ingestion agent either programmatically via the Agents API or using the Studio UI. Choose your preferred approach below:

Option A: Using the Agents API

Option B: Using the Studio UI

Alternatively, create the ingestion agent directly in the AI Studio:
  1. Navigate to Agents: Open the AI Studio and go to your project
  2. Create New Agent: Click the button and select Agent
  3. Configure Agent:
    • Key: ingestion-agent
    • Role: Prospect Ingestion Specialist
    • Description: Collects prospect information through natural conversation
    • Model: Select gpt-4o (or your preferred model)
  4. Add Instructions: Copy the system instructions from the section below into the Instructions field
  5. Configure Settings:
    • Max Iterations: 15
    • Max Execution Time: 300 seconds
  6. Add Tools: Include the Current Date tool for context awareness
  7. Save: Click Create Agent
System Instructions for Ingestion Agent:

2. Create the Qualification Agent

You can create the qualification agent either programmatically via the Agents API or using the Studio UI. Choose your preferred approach below:

Option A: Using the Agents API

Option B: Using the Studio UI

Alternatively, create the qualification agent directly in the AI Studio:
  1. Navigate to Agents: Go to your project in the AI Studio
  2. Create New Agent: Click the button and select Agent
  3. Configure Agent:
    • Key: qualification-agent
    • Role: Lead Qualification Specialist
    • Description: Analyzes prospect conversations and scores leads
    • Model: Select gpt-4o (or your preferred model)
  4. Add Instructions: Copy the system instructions from the section below into the Instructions field
  5. Configure Settings:
    • Max Iterations: 5
    • Max Execution Time: 300 seconds
  6. Tools: No tools needed for this agent (leave empty)
  7. Save: Click Create Agent
System Instructions for Qualification Agent:

3. Create the Insertion Agent

You can create the insertion agent either programmatically via the Agents API or using the Studio UI. Choose your preferred approach below:

Option A: Using the Agents API

Option B: Using the Studio UI

Alternatively, create the insertion agent directly in the AI Studio:
  1. Navigate to Agents: Go to your project in the AI Studio
  2. Create New Agent: Click the button and select Agent
  3. Configure Agent:
    • Key: insertion-agent
    • Role: CRM Insertion Specialist
    • Description: Validates and inserts qualified prospects into the CRM
    • Model: Select gpt-4o (or your preferred model)
  4. Add Instructions: Copy the system instructions from the section below into the Instructions field
  5. Configure Settings:
    • Max Iterations: 3
    • Max Execution Time: 300 seconds
  6. Add the CRM Insert Tool:
    • Click Add Tool and select Function
    • Tool Name: crminsert
    • Description: Insert qualified prospect into CRM
    • Function Parameters: Add the schema fields (company_name, contact_name, contact_email, etc.)
    • See the CRM Insert Tool Configuration section above for the complete JSON schema
  7. Save: Click Create Agent
System Instructions for Insertion Agent:
Learn more about tool configuration in Creating a Tool, and agent setup in Creating an Agent.

Integrating with the SDK

Choose your preferred programming language and install the corresponding SDK:
Get your integration ready by initializing the SDK as follows:
To implement the simplified 3-agent prospect qualification system:

Output

When going through the process, you’ll see the 3 agents at work:
  • The ingestion agent is progressively ingesting the conversation.
  • The qualifying agent transforms and validates all the conversation.
  • The insertion agent generates the correct payload.
Extract of the output at qualification and insertion time:

Viewing Logs

In the AI Studio, you can view the execution logs for each agent in your 3-agent prospect qualification system. Navigate to the Agents section and select each agent to view detailed logs of all runs, including:
  • Ingestion Agent Logs: View all multi-turn conversations with context preservation via previous_response_id
  • Qualification Agent Logs: Analyze the qualification analysis and scoring results
  • Insertion Agent Logs: Monitor tool execution and CRM insertion attempts
You can click on any log entry to view detailed information about the execution, including input parameters, agent responses, and any tool calls made during the run.
Log detail for a claude-opus-4 call showing the assistant recommending immediate sales engagement for a high-quality lead, with Rating, Defects, and Interactions feedback on the right.

Logs for the qualifying agent

To learn more about agents and their capabilities, see Agents API Documentation.

Key Benefits

  • Context Persistence: previous_response_id maintains conversation state across multiple turns without passing full history
  • Specialized Agents: Each agent optimized for its specific role (ingestion, qualification, insertion)
  • Modern Framework: Built on A2A Protocol for standardized agent-to-agent communication
  • Tool Integration: Seamless function tool integration for CRM and external system operations
  • Scalable Architecture: Independent agents enable parallel processing and easy optimization
Next Steps: Learn about Multi-Agent Workflows and Tool Support to expand your pipeline capabilities.