> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# エージェントをバッチでログする

> LLM Call が完了済みで、その記録だけが必要なフレームワーク向けに、エージェントのトレースを手動でログします。

export const AgentLensBanner = ({href}) => <Tip>
    <strong>This workflow is also available in CoreWeave Agent Lens.</strong> Agent Lens is the Forge experience built for tracing, monitoring, and analyzing AI agents, with automated insights into agent failures and user intents. It uses the same trace data as Weights & Biases Weave, so the traces you already send appear there with nothing to migrate.{' '}
    <a href={href || '/products/agent-lens'}>{href ? 'See how to do this in Agent Lens' : 'Learn about Agent Lens'}</a>.
  </Tip>;

<AgentLensBanner href="/ja/products/agent-lens/tracing/batch-logging" />

LLM Call が完了済みで、その記録だけが必要なフレームワークでは、`weave.log_turn` と `weave.log_conversation` を使用します。すべてのスパンは作成と同時に終了するため、コンテキストマネージャーを開いたままにしておく必要はありません。

この方法でログするデータは過去のものでも構いません。進行中の会話は必要ありません。`conversation_id` には、会話を一意に識別できる、変化しない任意の string を設定します。同じ `conversation_id` を持つターンは、**Agents** ビューで 1 つの会話としてまとめて表示されます。

独自のエージェントループを構築する場合は、[エージェントをトレースする](/ja/products/wandb/weave/guides/tracking/trace-agents) で説明しているリアルタイムのインストルメンテーション API を使用してください。

<h2 id="log-a-turn">
  ターンをログする
</h2>

完了したターンを 1 つ事後に記録するには、`weave.log_turn` を使用します。このメソッドは、すべての LLM スパンとツールスパンを含む完全な形式のターンを受け入れます。

<Tabs>
  <Tab title="Python">
    ```python lines highlight="1,3,28" theme={"system"}
    weave.init("[YOUR-TEAM]/[YOUR-PROJECT]")

    from weave.conversation import LLM, Message, Tool, Usage

    llm_span = LLM(
        model="gpt-4o",
        provider_name="openai",
        input_messages=[Message(role="user", content="What is the weather?")],
        output_messages=[Message(role="assistant", content="Let me check.")],
        usage=Usage(input_tokens=100, output_tokens=20),
    )

    tool_span = Tool(
        name="get_weather",
        arguments='{"city": "Tokyo"}',
        result='"24°C, sunny"',
    )

    llm_span2 = LLM(
        model="gpt-4o",
        provider_name="openai",
        input_messages=[Message(role="user", content="What is the weather?")],
        output_messages=[Message(role="assistant", content="It is 24°C and sunny.")],
        usage=Usage(input_tokens=150, output_tokens=30),
    )

    # すべてのスパンを含むターンをログします。
    weave.log_turn(
        conversation_id="my-conversation-abc",
        agent_name="weather-bot",
        messages=[
            Message(role="user", content="What is the weather in Tokyo?"),
            Message(role="assistant", content="It is 24°C and sunny in Tokyo."),
        ],
        spans=[llm_span, tool_span, llm_span2],
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```plaintext theme={"system"}
    This feature is not available in the TypeScript SDK yet.
    ```
  </Tab>
</Tabs>

`log_turn` は、出力されたスパンのトレース ID を含む `LogResult` を返します。

`log_turn` のオプションの `model` パラメーターは、子の LLM スパンではなく、ターン自体のスパンにモデルを設定します。各 `LLM` スパンは、それぞれ独立して独自の `model` を保持します。1 つのターンで複数のモデルを使用する場合は、そのターンのプライマリモデルとみなすものを `log_turn` の `model` に設定してください。

<h2 id="log-a-conversation">
  会話をログする
</h2>

マルチターンの会話全体を一括でインポートするには、`weave.log_conversation` を使用します。`turns` パラメーターは `Turn` オブジェクトのリストを受け入れます。各オブジェクトは、前述の `log_turn` の例と同じ方法で作成します。

<Tabs>
  <Tab title="Python">
    ```python lines theme={"system"}
    weave.log_conversation(
        conversation_id="my-conversation-abc",
        agent_name="weather-bot",
        turns=[turn_1, turn_2],
    )
    ```
  </Tab>

  <Tab title="TypeScript">
    ```plaintext theme={"system"}
    This feature is not available in the TypeScript SDK yet.
    ```
  </Tab>
</Tabs>


## Related topics

- [シグナルでエージェントをモニタリングする](/ja/products/wandb/weave/guides/tracking/view-agent-signals.md)
