> ## 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

> LLM クラスは、1 回の LLM API 呼び出しを chat スパンとして記録します。

```python theme={"system"}
class LLM(BaseModel): ...
```

1 回の LLM API 呼び出しを表します。OTel の chat スパンに対応します。

`LLM` は Pydantic モデルです。インスタンスの作成時に、以下の任意のフィールドをキーワード引数として指定できます。

`LLM` はコンテキストマネージャーであり、`with` 文で使用できます。

<h2 id="fields">
  フィールド
</h2>

| フィールド | タイプ | デフォルト |
| - | - | - |
| `model` | `str` | `''` |
| `provider_name` | `str` | `''` |
| `response_id` | `str` | `''` |
| `response_model` | `str` | `''` |
| `output_type` | `str` | `''` |
| `system_instructions` | `list[str]` | `[]` |
| `usage` | `Usage` | `Usage()` |
| `reasoning` | `Reasoning` | `Reasoning()` |
| `finish_reasons` | `list[str]` | `[]` |
| `input_messages` | `list[Message]` | `[]` |
| `output_messages` | `list[Message]` | `[]` |
| `media_attachments` | `list[MediaAttachment]` | `[]` |
| `request_temperature` | `Union[float, None]` | `None` |
| `request_max_tokens` | `Union[int, None]` | `None` |
| `request_top_p` | `Union[float, None]` | `None` |
| `request_top_k` | `Union[float, None]` | `None` |
| `request_frequency_penalty` | `Union[float, None]` | `None` |
| `request_presence_penalty` | `Union[float, None]` | `None` |
| `request_seed` | `Union[int, None]` | `None` |
| `request_stop_sequences` | `list[str]` | `[]` |
| `request_choice_count` | `Union[int, None]` | `None` |
| `started_at` | `Union[datetime, None]` | `None` |
| `ended_at` | `Union[datetime, None]` | `None` |

<h2 id="methods">
  メソッド
</h2>

<h3 id="output">
  output
</h3>

```python theme={"system"}
def output(content: str) -> LLM: ...
```

output\_messages にアシスタント メッセージを追加します。

<h3 id="think">
  think
</h3>

```python theme={"system"}
def think(content: str) -> LLM: ...
```

推論/思考の連鎖の内容を設定します。

<h3 id="attach_media">
  attach\_media
</h3>

```python theme={"system"}
def attach_media(
    *,
    content: Union[bytes, str] = '',
    uri: str = '',
    file_id: str = '',
    mime_type: str = '',
    modality: str = '',
) -> LLM: ...
```

この LLM Call にメディアを添付します。

ソースは 1 つだけ指定する必要があります。バイトは base64 エンコードされ、インラインの
GenAI blob として格納されます。文字列の content は、あらかじめ base64 エンコードしておく必要があります。URI と
プロバイダーのファイル ID は、GenAI の URI パートおよび file パートとして出力されます。

<h3 id="attach_media_url">
  attach\_media\_url
</h3>

```python theme={"system"}
def attach_media_url(url: str, *, modality: str = '') -> LLM: ...
```

この LLM Call にデータ URL または通常のメディア URI を添付します。

<h3 id="record">
  record
</h3>

```python theme={"system"}
def record(
    *,
    input_messages: Union[list[Message], None] = None,
    output_messages: Union[list[Message], None] = None,
    media_attachments: Union[list[MediaAttachment], None] = None,
    usage: Union[Usage, None] = None,
    reasoning: Union[Reasoning, str, None] = None,
    response_id: Union[str, None] = None,
    response_model: Union[str, None] = None,
    finish_reasons: Union[list[str], None] = None,
    output_type: Union[str, None] = None,
) -> LLM: ...
```

複数の LLM Call フィールドを 1 回の呼び出しで設定します。

手動でインストルメントされたエージェントでは通常、LLM Call の終了時に
8 個以上のフィールド (`input_messages`、`output_messages`、`usage`、
`response_id` など) を個別に代入して chat スパンを構築します。
`record(...)` を使うと、これらを 1 回のキーワード引数呼び出しにまとめられるため、
記録箇所を簡潔に保てます。

明示的に渡された (`None` 以外の) フィールドのみが適用され、
既存の値は保持されます。`reasoning` には
`Reasoning` インスタンスまたはプレーンな文字列 (自動的にラップされます) を指定できます。
メソッドチェーン用に `self` を返します。

<h3 id="end">
  end
</h3>

```python theme={"system"}
def end() -> None: ...
```

<h3 id="start">
  start
</h3>

```python theme={"system"}
def start() -> Self: ...
```

このスパンを 1 回だけ開始します。コンテキストマネージャーへのエントリ時も同じパスを使用します。

<h3 id="record_error">
  record\_error
</h3>

```python theme={"system"}
def record_error(error: BaseException) -> Self: ...
```

スパンを終了せずに失敗を記録します。スパンを終了する準備が整ったら `end()` を呼び出してください。

<h3 id="set_attributes">
  set\_attributes
</h3>

```python theme={"system"}
def set_attributes(attributes: dict[str, Any]) -> Self: ...
```

このスパンに任意の OTel 属性を付与します。

キーが 1 つでも複数でも dict を渡してください。キーが 1 つの場合は
`span.set_attributes({"weave.tag": "value"})` のように使用します。OTel の
`Span.set_attributes` に相当します。

`start_*` ファクトリ、`start()`、または `with` によってスパンが開始された後、
スパンが終了する前に呼び出す必要があります。この期間外に呼び出した場合は
no-op となり、警告がログに記録されます。バッチ取り込みの場合は、オブジェクトの宣言済みフィールドに
直接値を設定し、`log_turn` / `log_conversation` に渡してください。
