Knowledge

Answer questions from a PgVector knowledge base with Claude on Vertex AI.

The source uses claude-sonnet-4@20250514, which Anthropic lists as deprecated on Google Cloud. Google's Sonnet 4 card does not establish a completed retirement. For a new deployment, choose a current model and its supported settings, such as Sonnet 4.6. Provider availability and lifecycle dates can differ from the direct Anthropic API.

knowledge.py
"""Run `uv pip install ddgs sqlalchemy pgvector pypdf anthropic openai` to install dependencies."""

from agno.agent import Agent
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.models.vertexai.claude import Claude
from agno.vectordb.pgvector import PgVector

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge = Knowledge(
    vector_db=PgVector(
        table_name="recipes",
        db_url=db_url,
        embedder=OpenAIEmbedder(),
    ),
)
# Add content to the knowledge
knowledge.insert(url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf")

agent = Agent(
    model=Claude(id="claude-sonnet-4@20250514"),
    knowledge=knowledge,
)
agent.print_response("How to make Thai curry?", markdown=True)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass

Run the Example

Set up your virtual environment

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

Install dependencies

uv pip install -U agno "anthropic[vertex]" "psycopg[binary]" beautifulsoup4 openai pgvector pypdf sqlalchemy

Set up Google Cloud access

Use a project with billing and aiplatform.googleapis.com enabled, enable the chosen Claude model in Model Garden, and give your calling identity permission to use it. Follow Google's Claude setup guide. Install the Google Cloud CLI for the local sign-in command below.

Set the project and a location supported by your model. us-east5 is a supported example for Sonnet 4 and Sonnet 4.6; check the model's current availability before switching models or locations.

export ANTHROPIC_VERTEX_PROJECT_ID="your-project-id"
export CLOUD_ML_REGION="us-east5"

Authenticate locally with Application Default Credentials:

gcloud auth application-default login

ADC supplies credentials; it does not enable models or grant IAM permissions. These examples authenticate to Google Cloud without an Anthropic API key.

Set the embedding API key

The knowledge base uses OpenAI embeddings separately from the Vertex AI chat model.

export OPENAI_API_KEY="your_openai_api_key_here"

Run PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18

Run the example

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

python knowledge.py

Full source: cookbook/90_models/vertexai/claude/knowledge.py