# answer_with_cost

> **Function** in `deepagents_code`

📖 [View in docs](https://reference.langchain.com/python/deepagents-code/btw_cost/answer_with_cost)

Save completed usage before delivering an answer or finishing cancellation.

## Signature

```python
answer_with_cost(
    answer: Awaitable[str],
    *,
    thread_id: str,
    state: CostState,
) -> tuple[str, CostBreakdown | None]
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `answer` | `Awaitable[str]` | Yes | Tool-free side-question or thread-name generation. |
| `thread_id` | `str` | Yes | Thread that owns this request. |
| `state` | `CostState` | Yes | Checkpoint metadata used as a pricing fallback. |

## Returns

`tuple[str, CostBreakdown | None]`

Answer text and the persisted side-request subtotal, when available.

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/caaa7e7c12d214afa5cf0a1afed8eb6232aa6f7b/libs/code/deepagents_code/btw_cost.py#L192)