0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AIエージェントの「何が起きたか」を完全記録するPythonライブラリを作った

0
Posted at

作ったもの

AgentBlackBox — AIエージェントのフライトレコーダーです。

pip install agentblackbox

デコレータを1行追加するだけで、エージェントのLLMコール・ツール実行・エラー・コストをすべて記録します。

動機

AIエージェントを本番環境で動かしていると、こんな問題に必ずぶつかります:

  • エージェントが失敗したとき、どのツールコールが原因かわからない
  • 気づいたらAPIコストが数千円になっていた
  • 本番環境でしか再現しないバグがある

既存ツール(LangSmith、Weights & Biasesなど)はLangChainやOpenAI専用だったり、セットアップが重かったりします。

AgentBlackBoxはゼロ依存・フレームワーク非依存で、どんなエージェントにも1行で追加できます。

使い方

1. デコレータ(最もシンプル)

from agentblackbox import BlackBox

@BlackBox.record(agent_name="researcher")
def run_agent(task: str):
    # 既存のコードそのまま
    result = client.messages.create(
        model="claude-sonnet-4-6",
        messages=[{"role": "user", "content": task}]
    )
    return result.content[0].text

run_agent("今日のAIニュースをまとめて")

2. Anthropic SDK を自動計装(コード変更ゼロ)

from agentblackbox import patch_anthropic, BlackBox

patch_anthropic()  # これだけ。以降の全anthropic呼び出しを記録

with BlackBox.session("my-agent") as bb:
    run_my_existing_agent()  # 既存コード無変更

bb.replay()  # タイムラインを表示

3. タイムラインのリプレイ

======================================================================
  Session: a1b2c3d4-...
  Agent:   researcher
  Status:  success
  Cost:    $0.002341
======================================================================

[14:23:01.234] LLM  model=claude-sonnet-4-6  tokens=523→891  $0.002341  1203.4ms
  IN : 今日のAIニュースをまとめて
  OUT: 本日のAI関連ニュースをまとめます...

[14:23:02.891] TOOL [✓] web_search  234.1ms
  ARGS  : {"query": "AI news today"}
  RESULT: [{"title": "OpenAI announces..."}]

4. Webダッシュボード

pip install "agentblackbox[dashboard]"
agentblackbox dashboard
# → http://localhost:8765

対応SDK

SDK 自動計装
Anthropic Python SDK patch_anthropic()
OpenAI Agents SDK patch_openai_agents()
LangChain BlackBoxCallbackHandler
その他(手動) bb.record_llm_call()

クラウドモード(チーム共有)

from agentblackbox.remote import RemoteStorage
from agentblackbox import BlackBox

store = RemoteStorage(
    api_key="abx_...",
    endpoint="https://your-dashboard.example.com",
)
with BlackBox.session("prod-agent", storage=store) as bb:
    run_agent()

インストール

# コアのみ(依存ゼロ)
pip install agentblackbox

# ダッシュボード付き
pip install "agentblackbox[dashboard]"

Python 3.10+ 対応。

まとめ

AIエージェントを真剣に本番運用しようとすると、可観測性は必須です。AgentBlackBoxはその最小構成を提供します。

GitHubスターいただけると励みになります 🙏

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?