Analyze a codebase with Gemini in Vertex AI

Source notebook

Repo path: gemini/use-cases/code/analyze_codebase.ipynb · Open on GitHub · intermediate

Uses Gemini 3.5 Flash on Vertex AI to analyze, document, debug, and extend a GitHub codebase.

Summary

This notebook teaches how to load an entire GitHub repository into Gemini’s long-context prompt using Gitingest and analyze it through Vertex AI. It demonstrates cloning Online Boutique, creating a context cache, asking Gemini for summaries, onboarding guides, bug findings, fixes, reliability and security recommendations, quizzes, quickstarts, and changelog summaries. It also shows function calling to extract a GitHub issue and use that feature request with the cached code context.

Key code patterns

Create GenAI client

from google import genai
 
client = genai.Client(
    enterprise=True,
    project=PROJECT_ID,
    location=LOCATION,
)

Initializes the Google GenAI SDK client for Vertex AI with project and region.

Clone and ingest repo

clone_repo(repo_url, repo_dir)
 
_, tree, content = ingest(repo_dir)

Turns a cloned repository into a directory tree and concatenated code content for long-context prompting.

Cache codebase context

cached_content = client.caches.create(
    model=MODEL_ID,
    config=CreateCachedContentConfig(
        contents=contents,
        system_instruction=system_instruction,
        ttl="3600s",
    ),
)

Stores the large codebase context once to reduce repeated prompt cost and latency.

Generate with cached context

response = client.models.generate_content(
    model=MODEL_ID,
    contents=question,
    config=GenerateContentConfig(
        cached_content=cached_content.name,
    ),
)

Reuses cached codebase context for non-streaming analysis prompts.

Stream generated answers

responses = client.models.generate_content_stream(
    model=MODEL_ID,
    contents=question,
    config=GenerateContentConfig(
        cached_content=cached_content.name,
    ),
)
 
for response in responses:
    print(response.text, end="")

Streams longer generated guides, bug analyses, and recommendations.

Function calling for GitHub issue

response = client.models.generate_content(
    model=MODEL_ID,
    contents=question,
    config=GenerateContentConfig(
        tools=[get_github_issue],
    ),
)

Uses a Python function as a tool to retrieve a GitHub issue from a feature request URL.

Models & APIs used

  • Models: gemini-3.5-flash
  • APIs / services: Vertex AI, GitHub API
  • SDKs / libraries: google-genai, gitingest, gitpython, PyGithub

When to use this

Use this pattern when a large repository needs Gemini-assisted summarization, onboarding, debugging, feature planning, or changelog analysis.

Gotchas & caveats

  • Vertex AI API must be enabled in an existing Google Cloud project.
  • Colab users must authenticate and restart the runtime after installing packages.
  • LOCATION defaults to us-central1 from GOOGLE_CLOUD_REGION when not set.
  • Context caching is only available for stable models with fixed versions and requires a version postfix according to the notebook note.
  • Tools cannot be added at runtime when using cached content, so the function-calling step does not use the cache.

Best practices

  • Use a shallow clone when loading the GitHub repository.
  • Create a context cache for the codebase to avoid resending the full context on every request.
  • Include a system instruction that frames the model as a coding expert for code-related questions.
  • Use streaming generation for longer developer guides, troubleshooting guides, and recommendations.
  • Separate tool-based GitHub issue retrieval from cached-content prompts because tools cannot be added at runtime with cached content.