Dynamically selecting the model that claude-code-action should run with
Hi 👋 thanks a lot for this great project - it's a really nice use case to be able to run Claude Code in a repository!
Is there a way to dynamically select the model that Claude Code should use by specifying it in the comment? For example like so:
@claude opus please fix this bug
I've tried the following configuration but without success:
name: Claude Code
on:
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]
issues:
types: [opened, assigned]
pull_request_review:
types: [submitted]
jobs:
claude:
if: |
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
runs-on: ubuntu-latest
needs: install-dependencies
permissions:
contents: read
pull-requests: read
issues: read
id-token: write
actions: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Parse model from comment
id: parse-model
run: |
COMMENT_BODY="${{ github.event.comment.body || github.event.review.body || github.event.issue.body || github.event.issue.title }}"
# Simple syntax: @claude opus @claude sonnet
if echo "$COMMENT_BODY" | grep -qi "\b@claude\s+opus\b"; then
MODEL="claude-opus-4-20250514"
echo "model=$MODEL" >> $GITHUB_OUTPUT
echo "Using model: $MODEL"
elif echo "$COMMENT_BODY" | grep -qi "\b@claude\s+sonnet\b"; then
MODEL="claude-sonnet-4-20250514"
echo "model=$MODEL" >> $GITHUB_OUTPUT
echo "Using model: $MODEL"
fi
- name: Run Claude Code
id: claude
uses: anthropics/claude-code-action@beta
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
model: ${{ steps.parse-model.outputs.model }}
additional_permissions: |
actions: read
claude_env: |
ANTHROPIC_BASE_URL: ${{ vars.ANTHROPIC_BASE_URL }}
When I then comment and see Claude Code running in GH, it defaults back to Sonnet:
For anyone looking for something that does this, this code snippet below works for me.
You can invoke a model by writing opus or sonnet as the first word after claude.
Only downside to this is that user-message passed to the model includes the word opus or sonnet. It would be an improvement if this was supported OOTB.
name: Claude Code
on:
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]
issues:
types: [opened, assigned]
pull_request_review:
types: [submitted]
jobs:
claude:
if: |
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
issues: read
id-token: write
actions: read # Required for Claude to read CI results on PRs
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Pick model from trigger body
id: model
env:
COMMENT_BODY: ${{ github.event.comment.body }}
REVIEW_BODY: ${{ github.event.review.body }}
ISSUE_BODY: ${{ github.event.issue.body }}
ISSUE_TITLE: ${{ github.event.issue.title }}
run: |
body=$(printf '%s\n%s\n%s\n%s\n' "$COMMENT_BODY" "$REVIEW_BODY" "$ISSUE_BODY" "$ISSUE_TITLE" | tr '[:upper:]' '[:lower:]')
if printf '%s' "$body" | grep -Eq '@claude[[:space:]]+opus\b'; then
echo "model=opus" >> "$GITHUB_OUTPUT"
elif printf '%s' "$body" | grep -Eq '@claude[[:space:]]+sonnet\b'; then
echo "model=sonnet" >> "$GITHUB_OUTPUT"
else
echo "model=" >> "$GITHUB_OUTPUT"
fi
- name: Run Claude Code
id: claude
uses: anthropics/claude-code-action@v1
with:
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
# This is an optional setting that allows Claude to read CI results on PRs
additional_permissions: |
actions: read
# Optional: Give a custom prompt to Claude. If this is not specified, Claude will perform the instructions specified in the comment that tagged it.
# prompt: 'Update the pull request description to include a summary of changes.'
# Pass --model when the trigger body mentions "opus" or "sonnet"; otherwise use the action default.
claude_args: ${{ steps.model.outputs.model != '' && format('--model {0}', steps.model.outputs.model) || '' }}