📝

Codex CLIを使い始めました

に公開

Codex CLIを使い始めました

今までAPIでの従量課金のため使っていませんでしたが、ChatGPT Planのユーザーは定額制なので使い始めました
https://x.com/OpenAIDevs/status/1953559797883891735

設定ファイルなどについて模索中ですが、随時更新していこうかと思います。

準備

Web検索をさせるためにgemini-cliを入れる

gemini-cliをインストールして、ログインを済ませておく
https://github.com/google-gemini/gemini-cli

コマンド例

$ npm install -g @google/gemini-cli
$ gemini

Web検索させるためにBrave Search APIを入手しておく

https://brave.com/ja/search/api/

通知が来るようにする (Mac)

https://christina04.hatenablog.com/entry/enable-osascript-notification
こちらを参考に、スクリプトエディタ.appを開いて事前に通知を1回走らせて通知を許可しないと、通知が来ないので注意。

display notification "通知メッセージ" with title "codex"

serenaでbrowserを開かなくする

diff -u ~/.serena/serena_config.yml.orig ~/.serena/serena_config.yml

--- ~/.serena/serena_config.yml.orig        2025-08-13 17:48:17
+++ ~/.serena/serena_config.yml     2025-08-13 17:47:06
@@ -20,7 +20,7 @@
 # shows Serena's current session logs - as an alternative to the GUI log window which
 # is supported on all platforms.
 
-web_dashboard_open_on_launch: true
+web_dashboard_open_on_launch: false
 # whether to open a browser window with the web dashboard when Serena starts (provided that web_dashboard
 # is enabled). If set to False, you can still open the dashboard manually by navigating to
 # http://localhost:24282/dashboard/ in your web browser (24282 = 0x5EDA, SErena DAshboard).

設定ファイル

~/.codex/AGENTS.md

# AGENTS

## Basic Policy

- Please provide all answers in Japanese
- Do not mention previous changes in comments

## Web Search Policy

### 1. Search Tools

- Default: **Always** use `gemini -p` first, including fresh news queries (e.g., "today's events").
- Fallback: Use **Brave Search** via the MCP servers integration **only if** the Gemini CLI call for the current query returns an error (e.g., `429 Resource exhausted`, network/API failure) or Gemini CLI is unavailable on this machine.

### 2. Prompt Format

When instructing Codex or CLAUDE mode to perform a web search, use the following format:

```sh
gemini -p "WebSearch: <your search query>"
```

You can also use a multi-line prompt to enforce structured output:

```sh
gemini -p $'Please meet the following requirements:\n\
- Always perform a web search and list at least 3 source URLs\n\
- Summarize important points in bullet points\n\
Topic to research: "<your topic>"'
```

By explicitly specifying `gemini -p` (or using `brave-search` when appropriate), it becomes easier to obtain **responses that use the latest web search results**.

### 3. Error Handling

- If a `429 Resource exhausted` error occurs when using Gemini CLI, do not retry immediately; instead, abort the attempt to avoid hitting rate limits.
- When an API error occurs (including `429` or other failures), fall back to using Brave Search via MCP servers as the alternative search method.
- Do **not** preemptively select Brave based on query type or prior failures; require an actual Gemini error during the current attempt before falling back.

### 4. Output Requirements

- Answers must include sources, making them more reliable and easier to verify.
- Following these rules helps stabilize prompt quality for Codex.

~/.codex/config.toml

  • braveのAPI Keyは書き換えてください
# 通知
# 通知をONにする方法 https://christina04.hatenablog.com/entry/enable-osascript-notification
# スクリプトエディタ.appを開いて、以下のdisplay notification コードを実行して通知設定をいじれるようにする
notify = [
    "bash",
    "-lc",
    "JSON=\"$1\"; LAST_MESSAGE=$(echo \"$JSON\" | jq -r '.\"last-assistant-message\" // \"Codex task completed\"'); osascript -e \"display notification \\\"$LAST_MESSAGE\\\" with title \\\"Codex\\\"\"",
    "notify",
]

[mcp_servers.brave-search]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-brave-search"]
env = { "BRAVE_API_KEY" = "<API Key>" }

[mcp_servers.serena]
command = "uvx"
args = [
    "--from",
    "git+https://github.com/oraios/serena",
    "serena",
    "start-mcp-server",
    "--context",
    "codex",
]

[mcp_servers.context7]
command = "npx"
args = ["-y", "@upstash/context7-mcp"]

[mcp_servers.playwright]
command = "npx"
args = ["-y", "@playwright/mcp@latest"]

開発

同じRepositoryで複数作業する

git worktreeを使う

作業途中がなく、差分は全てcommit済みの場合

なおかつcommitもする場合

コマンド例

# 新規でディレクトリを作成
git worktree -b <新しいbranch名> <新しいディレクトリ名> HEAD

# 作業開始
code <新しいディレクトリ名>

# 諸々作業
# commitなど

# 作業完了
git merge <新しいbranch名>

# 作業ディレクトリを削除
git worktree remove <作業していたディレクトリ名>

作業途中で、commitしていない差分がある場合

なおかつcommitはしたくない場合

コマンド例

# stashに作業途中のファイルを退避
git stash push -u -m wip

# 新規でディレクトリを作成
git worktree -b <新しいbranch名> <新しいディレクトリ名> HEAD

# 作業開始
code <新しいディレクトリ名>

# stashを適用 (新しいディレクトリ内で)
git stash apply stash@{0}   # or: git stash pop

# 諸々作業

# stashに作業途中のファイルを退避 (新しいディレクトリ内で)
git stash push -u -m wip

# stashを適用
git stash apply stash@{0}   # or: git stash pop

# 作業ディレクトリを削除
git worktree remove <作業していたディレクトリ名>

トラブルシューティング

? Codex wants to run が出て画面が固まったように見える

TUIが1画面に収まりきれず下記の選択メニューが表示されていない可能性がある。
問題がなければ空Enterや方向キーで操作する。

▌Allow command?
▌ Yes   Always   No 
▌ Approve and run the command

参考

https://blog.lai.so/codex-rs-intro/
https://zenn.dev/mizchi/articles/gemini-cli-for-google-search
https://christina04.hatenablog.com/entry/enable-osascript-notification
https://zenn.dev/yourstand_blog/articles/codex-cli-by-claude-code-user
https://zenn.dev/soramarjr/articles/c0210f128a4d2a

備考

brave-searchのMCPにdockerを使う方法は下記のメッセージが出てなぜかうまく行きませんでした。

🖐  MCP client for `brave-search` failed to start: request timed out
ユカイ工学テックブログ

Discussion