> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agno.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sales Pipeline Forecaster

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

```python sales_pipeline.py theme={null}
"""
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,
    )
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno google-api-python-client google-auth google-auth-httplib2 google-auth-oauthlib openai
    ```
  </Step>

  <Step title="Export your API keys">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      export SALES_PIPELINE_SHEET_ID="your_sales_pipeline_sheet_id_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      $Env:SALES_PIPELINE_SHEET_ID="your_sales_pipeline_sheet_id_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Save the code above as `sales_pipeline.py`, then run:

    ```bash theme={null}
    python sales_pipeline.py
    ```
  </Step>
</Steps>

Full source: [cookbook/91\_tools/google/sheets/sales\_pipeline.py](https://github.com/agno-agi/agno/blob/main/cookbook/91_tools/google/sheets/sales_pipeline.py)
