Skip to content

feat(agent): Gemini/Anthropic を Vertex AI(SA→ADC)経由に移行(ADR-0015)#401

Merged
yusuke0610 merged 5 commits into
mainfrom
feat/vertex-ai-llm-providers
Jun 24, 2026
Merged

feat(agent): Gemini/Anthropic を Vertex AI(SA→ADC)経由に移行(ADR-0015)#401
yusuke0610 merged 5 commits into
mainfrom
feat/vertex-ai-llm-providers

Conversation

@yusuke0610

@yusuke0610 yusuke0610 commented Jun 23, 2026

Copy link
Copy Markdown
Owner

Summary

職務経歴書(PII)を扱う本番環境として、Gemini Developer API の無料枠(学習リスク)を廃止。Gemini と Anthropic を Vertex AI(Cloud Run の SA → ADC)に切り替え、シークレットを OpenAI キー 1 本に削減。

  • Google Gemini: asia-northeast1(Tokyo)で Vertex 経由に
  • Anthropic Claude: asia-southeast1(Singapore)で Vertex 経由に(Tokyo 未提供のため最寄り)
  • OpenAI GPT: API キー継続(GCP に無い)

技術変更

  • google_client.py: genai.Client(vertexai=True, project, location)
  • anthropic_client.py: AsyncAnthropicVertex(project_id, region)
  • model_catalog.py: Haiku を Vertex 版 ID(claude-haiku-4-5@20251001)に統一
  • 環境変数:ANTHROPIC_API_KEY / GOOGLE_API_KEY 廃止、VERTEX_LOCATION / VERTEX_ANTHROPIC_LOCATION 追加
  • requirements.txt: anthropic[vertex] で ADC 対応
  • infra(cloud_run): 注入を OpenAI キーのみに絞り込み(secret コンテナ削除は別 PR)

検証

  • make ci (lint + test): pass ✅
  • Backend テスト: 582 green ✅
  • OpenAPI drift: なし ✅
  • infra fmt-check: pass ✅

トレードオフ

  • regional endpoint 課金: Claude の Singapore regional は global 比 +10% プレミアム(PII 保護優先)
  • ローカル・CI: LLM_LOCAL_OLLAMA=1 で Vertex を避ける(ADC 不要)。実 Vertex をローカルで試す場合のみ gcloud auth application-default login
  • 要・実デプロイ確認: asia-southeast1 での Claude 提供と model id は実デプロイで確認(pytest/CI smoke では Vertex 実呼び出し未検証)

関連

  • ADR-0015: Vertex AI / SA→ADC / リージョン戦略 / トレードオフを新規に起票
  • ADR-0013(マルチプロバイダ)の認証部分を更新

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Switched Gemini and Claude/Anthropic LLM access to Vertex AI using application default credentials (no API keys).
    • Updated configuration: removed ANTHROPIC_API_KEY / GOOGLE_API_KEY; added VERTEX_LOCATION and VERTEX_ANTHROPIC_LOCATION (with defaults).
    • Updated Claude Haiku to the versioned Vertex-compatible model ID.
  • Documentation
    • Added ADR-0015 and updated LLM environment variable docs.
  • Tests
    • Updated LLM client and billing expectations for Vertex and the versioned model ID.
  • Infra
    • Updated Cloud Run secret/env injection behavior and Cloudflare CNAME resource/TTL handling.
    • Refreshed Terraform/OpenTofu provider lock files.

職務経歴書(PII)の学習リスク回避のため Gemini Developer API 無料枠を廃し、
Gemini と Anthropic を Vertex AI(Cloud Run の SA → ADC)経由に切り替える。
OpenAI のみ GCP に存在しないため API キーを継続する。

- google_client: genai.Client(vertexai=True, project, location) 化
- anthropic_client: AsyncAnthropicVertex(project_id, region) 化(generate は無変更)
- model_catalog: Haiku を Vertex 版 ID(claude-haiku-4-5@20251001)に
  (Sonnet 4.6 / Gemini は同 ID)
- settings/env_keys: API キー getter を GCP_PROJECT_ID + VERTEX_LOCATION /
  VERTEX_ANTHROPIC_LOCATION に置換(Gemini=Tokyo / Claude=Singapore)
- requirements: anthropic[vertex](ADC 用 google-auth)
- infra(cloud_run): ANTHROPIC_API_KEY/GOOGLE_API_KEY 注入停止・Vertex location 注入追加
  (secret コンテナ削除は別 PR)
- docker-compose / docs/api.md 更新・ADR-0015 起票
- tests: key 未設定→project 未設定に置換、billing の Haiku model_id assert を更新

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9d871ef7-3a0f-4213-977a-80ae315200fc

📥 Commits

Reviewing files that changed from the base of the PR and between 10f5fae and 84e503f.

📒 Files selected for processing (3)
  • docs/adr/0015-vertex-ai-for-gemini-anthropic.md
  • infra/modules/cloud_run/variables.tf
  • infra/modules/cloudflare/main.tf
✅ Files skipped from review due to trivial changes (1)
  • docs/adr/0015-vertex-ai-for-gemini-anthropic.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • infra/modules/cloud_run/variables.tf
  • infra/modules/cloudflare/main.tf

📝 Walkthrough

Walkthrough

Migrates Gemini and Anthropic LLM clients to Vertex AI using GCP project and region configuration, updates related docs, tests, and runtime wiring, versions the Haiku model ID, and refreshes Terraform provider locks plus Cloudflare resource naming.

Changes

Vertex AI Migration

Layer / File(s) Summary
Env and settings contract
backend/app/core/env_keys.py, backend/app/core/settings.py, backend/requirements.txt
Removes Anthropic/Google API-key env access, adds GCP project and Vertex location getters, and enables the Anthropic Vertex extra dependency.
GCP project validation
backend/app/services/agent/llm/base.py
Adds require_gcp_project for empty-project validation.
Vertex client initialization
backend/app/services/agent/llm/anthropic_client.py, backend/app/services/agent/llm/google_client.py
Switches both LLM clients to Vertex AI initialization with GCP project and region settings.
Haiku model ID update
backend/app/services/agent/model_catalog.py
Pins the Haiku model ID to claude-haiku-4-5@20251001.
Cloud Run and local env wiring
infra/modules/cloud_run/variables.tf, infra/modules/cloud_run/main.tf, docker-compose.yml
Adds Vertex region inputs, injects Vertex location env vars, and changes secret injection to match the new provider setup.
Docs and test updates
docs/adr/0015-vertex-ai-for-gemini-anthropic.md, docs/api.md, backend/tests/test_llm_clients.py, backend/tests/test_billing.py
Documents the Vertex migration and updates client and billing tests for the new auth and model ID behavior.

Infrastructure Maintenance

Layer / File(s) Summary
Terraform provider repins
infra/environments/dev/.terraform.lock.hcl, infra/environments/prod/.terraform.lock.hcl, infra/environments/stg/.terraform.lock.hcl
Updates Cloudflare, Google, and Turso provider versions, constraints, and hashes across environments.
Cloudflare DNS resource rename
infra/modules/cloudflare/main.tf
Renames the Cloudflare DNS record resource and adds a moved state block.
AGENTS simplification
AGENTS.md
Replaces the detailed guide with a single pointer to .claude/CLAUDE.md.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • yusuke0610/devforge#331: Shares the same LLM configuration surface in env_keys.py, settings.py, and the Google/Anthropic client wiring.
  • yusuke0610/devforge#321: Touches the same LLM env and Anthropic client areas that this PR rewires for Vertex AI.
  • yusuke0610/devforge#332: Modifies the same anthropic_client.py and shared LLM helper path used here.

Suggested labels

feature

Poem

🐇 Hop, hop, the keys are gone,
Vertex regions shine at dawn.
GCP and ADC align,
Claude and Gemini work just fine.
One little rabbit gives a cheer,
“Keyless clouds are now right here!”

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Gemini/Anthropic の Vertex AI への移行と ADR-0015 を端的に示しており、変更内容の मुख्य点と一致しています。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/vertex-ai-llm-providers

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docker-compose.yml`:
- Around line 42-43: The VERTEX_LOCATION and VERTEX_ANTHROPIC_LOCATION
environment variables in docker-compose.yml are using bare variable substitution
syntax without default values, which causes unset variables to become empty
strings instead of allowing the backend to apply its own fallback defaults.
Modify both variable references to include default values using the Docker
Compose syntax of ${VARIABLE_NAME:-default_value}, ensuring that when these
environment variables are not set, appropriate default values are used instead
of empty strings to prevent Vertex clients from being initialized with blank
locations.

In `@infra/modules/cloud_run/main.tf`:
- Around line 12-16: The deprecated `anthropic-api-key` and `google-api-key`
entries are still present in `local.secret_names`, which causes unnecessary IAM
permissions to be granted to the Cloud Run service account even though these
secrets are no longer used after the Vertex AI migration. Remove both the
`anthropic-api-key` and `google-api-key` strings from the `local.secret_names`
list to follow least privilege IAM principles and ensure the Cloud Run service
account only has access to the secrets it actually needs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e7aec2cd-6897-4d2b-ae41-b8e4c8f49a52

📥 Commits

Reviewing files that changed from the base of the PR and between 9fc6f10 and 609eaf5.

📒 Files selected for processing (14)
  • backend/app/core/env_keys.py
  • backend/app/core/settings.py
  • backend/app/services/agent/llm/anthropic_client.py
  • backend/app/services/agent/llm/base.py
  • backend/app/services/agent/llm/google_client.py
  • backend/app/services/agent/model_catalog.py
  • backend/requirements.txt
  • backend/tests/test_billing.py
  • backend/tests/test_llm_clients.py
  • docker-compose.yml
  • docs/adr/0015-vertex-ai-for-gemini-anthropic.md
  • docs/api.md
  • infra/modules/cloud_run/main.tf
  • infra/modules/cloud_run/variables.tf

Comment thread docker-compose.yml Outdated
Comment thread infra/modules/cloud_run/main.tf Outdated
yusuke0610 and others added 3 commits June 23, 2026 16:54
versions.tf が cloudflare ~> 5.0 を要求しているのに lock が v4 系(4.52.7)を
pin したままで、infra を触る PR の opentofu-validate が provider 解決段階で
失敗していた(main 由来の既存ドリフト。本 PR が初の infra 変更で顕在化)。

- cloudflare provider を v5.21.0 に更新(turso 1.2.0 / google 7.37.0 も追従)
- cloudflare_record → cloudflare_dns_record(v5 改名)、value → content、
  ttl=1 を明示(v5 で必須)。use_custom_domain は全環境 false で count=0 の
  ため移行対象インスタンスは無く moved ブロックは不要
- dev/stg/prod の lock を linux_amd64 + darwin_arm64 の両プラットフォームで再生成

make infra-validate(3 環境)/ infra-fmt-check 済み。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- docker-compose: VERTEX_LOCATION / VERTEX_ANTHROPIC_LOCATION に :-既定値 を付与。
  bare の ${VAR} は未設定時に空文字を注入し backend の os.getenv フォールバックを
  潰すため(Vertex client が空ロケーションで初期化される)
- cloud_run: Vertex 移行で未使用になった anthropic-api-key / google-api-key を
  secret_names から削除(コンテナと secretAccessor を destroy / least privilege)。
  本 PR で注入廃止と同時に削除する方針に変更(当初の別 PR 分離を取りやめ)

make infra-validate(3 環境)/ infra-fmt-check 済み。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@yusuke0610

Copy link
Copy Markdown
Owner Author

@coderabbitai RVして

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown

@yusuke0610 了解です!PR #401 のレビューを開始します 🐇

✅ Action performed

Full review finished.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/adr/0015-vertex-ai-for-gemini-anthropic.md`:
- Line 47: The ADR statement at line 47 describing the deprecated secret
retention strategy is inconsistent with the current infrastructure
implementation in infra/modules/cloud_run/main.tf, which already removes
anthropic-api-key and google-api-key from the secret_names list. Update the ADR
documentation to reflect the actual current state where these secrets are
already being excluded and will be deleted on the next terraform apply, rather
than stating they will be retained for deletion in a separate PR.

In `@infra/modules/cloud_run/variables.tf`:
- Around line 67-77: Add validation blocks to both the vertex_location and
vertex_anthropic_location variables to ensure they only accept non-empty string
values. Use a Terraform validation condition that checks the string length is
greater than zero, with an error message indicating that these variables cannot
be empty, as empty values would prevent proper Vertex AI client initialization
in Cloud Run.

In `@infra/modules/cloudflare/main.tf`:
- Around line 19-29: Add a moved block to the infra/modules/cloudflare/main.tf
file to handle the resource migration from cloudflare_record.app to
cloudflare_dns_record.app. The moved block should map the old resource state
(cloudflare_record.app) to the new resource type (cloudflare_dns_record.app) to
prevent Terraform from destroying and recreating the DNS record during the
upgrade to Cloudflare provider v5. Place this moved block in the same file where
the cloudflare_dns_record.app resource is defined.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2956ce6c-ea41-41c8-ac80-792f0dae66ea

📥 Commits

Reviewing files that changed from the base of the PR and between 9fc6f10 and 10f5fae.

📒 Files selected for processing (19)
  • AGENTS.md
  • backend/app/core/env_keys.py
  • backend/app/core/settings.py
  • backend/app/services/agent/llm/anthropic_client.py
  • backend/app/services/agent/llm/base.py
  • backend/app/services/agent/llm/google_client.py
  • backend/app/services/agent/model_catalog.py
  • backend/requirements.txt
  • backend/tests/test_billing.py
  • backend/tests/test_llm_clients.py
  • docker-compose.yml
  • docs/adr/0015-vertex-ai-for-gemini-anthropic.md
  • docs/api.md
  • infra/environments/dev/.terraform.lock.hcl
  • infra/environments/prod/.terraform.lock.hcl
  • infra/environments/stg/.terraform.lock.hcl
  • infra/modules/cloud_run/main.tf
  • infra/modules/cloud_run/variables.tf
  • infra/modules/cloudflare/main.tf

Comment thread docs/adr/0015-vertex-ai-for-gemini-anthropic.md Outdated
Comment thread infra/modules/cloud_run/variables.tf
Comment thread infra/modules/cloudflare/main.tf
- cloudflare: cloudflare_record → cloudflare_dns_record の改名に moved block を追加。
  全環境 use_custom_domain=false(count=0)で現状インスタンスは無いが、有効化済み
  state での destroy/recreate(DNS ダウンタイム)を防ぐ防御策
- cloud_run: vertex_location / vertex_anthropic_location に空文字を弾く validation を追加
  (空文字注入で backend のデフォルト補完が潰れるのを防ぐ)
- ADR-0015: 未使用シークレットを「残置(別PR削除)」→「本PRで削除」に記述を実装と整合

make infra-validate(3 環境)/ infra-fmt-check 済み。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@yusuke0610
yusuke0610 merged commit 7890f46 into main Jun 24, 2026
19 checks passed
@yusuke0610
yusuke0610 deleted the feat/vertex-ai-llm-providers branch July 20, 2026 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant