Sales Pipeline Forecaster

Read a deals spreadsheet, calculate weighted pipeline by stage, and forecast revenue.

sales_pipeline.py
"""
Sales Pipeline Forecaster
=========================

Read a deals spreadsheet, calculate weighted pipeline by stage, and forecast revenue.

Setup:
  1. Create a Google Sheet with columns: Deal Name, Company, Amount, Stage, Close Date, Probability
  2. Set SALES_PIPELINE_SHEET_ID env var to your spreadsheet ID
  3. Set Google OAuth credentials (GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET)

Example Sheet Format:
  | Deal Name    | Company   | Amount  | Stage        | Close Date | Probability |
  |--------------|-----------|---------|--------------|------------|-------------|
  | Enterprise   | Acme Corp | 50000   | Negotiation  | 2026-07-15 | 70%         |
  | Starter Plan | Beta Inc  | 5000    | Discovery    | 2026-08-01 | 20%         |

Run:
  .venvs/demo/bin/python cookbook/91_tools/google/sheets/sales_pipeline.py
"""

from os import getenv

from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.tools.google.sheets import GoogleSheetsTools
from pydantic import BaseModel, Field


class PipelineForecast(BaseModel):
    total_pipeline: float = Field(..., description="Sum of all deal amounts")
    weighted_pipeline: float = Field(
        ..., description="Sum of amount * probability for each deal"
    )
    deals_by_stage: dict[str, int] = Field(..., description="Count of deals per stage")
    top_deals: list[str] = Field(..., description="Top 3 deals by weighted value")
    forecast_summary: str = Field(..., description="Brief forecast narrative")


agent = Agent(
    name="Pipeline Forecaster",
    model=OpenAIResponses(id="gpt-5.5"),
    tools=[GoogleSheetsTools(read_sheet=True)],
    instructions=[
        "You analyze sales pipeline data and provide revenue forecasts.",
        "Calculate weighted pipeline as: sum of (deal amount * probability) for each deal.",
        "Group deals by stage and identify the highest-value opportunities.",
        "Provide actionable insights about pipeline health.",
    ],
    output_schema=PipelineForecast,
    markdown=True,
)

if __name__ == "__main__":
    sheet_id = getenv("SALES_PIPELINE_SHEET_ID")
    if not sheet_id:
        print("Set SALES_PIPELINE_SHEET_ID to your spreadsheet ID")
        print(
            "Example: export SALES_PIPELINE_SHEET_ID=1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms"
        )
        exit(1)

    agent.print_response(
        f"Analyze the sales pipeline in spreadsheet {sheet_id} and provide a revenue forecast. "
        "Calculate the weighted pipeline value and identify our top opportunities.",
        stream=True,
    )

Add the actual tab and range to the prompt, for example Read 'Deals'!A1:F100 from the spreadsheet before calculating the forecast. read_sheet requires both the spreadsheet ID and a range, and the toolkit has no tab-discovery method. The output schema structures the model's calculations; it does not independently validate arithmetic.

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno google-api-python-client google-auth google-auth-httplib2 google-auth-oauthlib openai

Configure Google OAuth

Enable the Google API used by this example, configure the consent screen, and create a Desktop OAuth client in your Cloud project. Export its GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, and GOOGLE_PROJECT_ID, or place the downloaded client JSON at credentials.json in the directory where you run Python. Google's Python quickstart shows the Desktop client setup.

The first tool call opens a browser for consent and caches credentials in token.json. Use a separate token_path when switching accounts. Passing an Agno user_id does not switch the authenticated Google account.

Export environment variables

export OPENAI_API_KEY="your_openai_api_key_here"
export SALES_PIPELINE_SHEET_ID="your_sales_pipeline_sheet_id_here"

Run the example

Save the code above as sales_pipeline.py, then run:

python sales_pipeline.py

Full source: cookbook/91_tools/google/sheets/sales_pipeline.py