Skip to content

openai[patch],anthropic[patch]: Populate usage_metadata on invoke, add docs #5632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions docs/core_docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ docs/how_to/tools_prompting.md
docs/how_to/tools_prompting.mdx
docs/how_to/tools_builtin.md
docs/how_to/tools_builtin.mdx
docs/how_to/tool_calls_multi_modal.md
docs/how_to/tool_calls_multi_modal.mdx
docs/how_to/tool_calling.md
docs/how_to/tool_calling.mdx
docs/how_to/structured_output.md
Expand Down Expand Up @@ -107,6 +105,10 @@ docs/how_to/output_parser_fixing.md
docs/how_to/output_parser_fixing.mdx
docs/how_to/multiple_queries.md
docs/how_to/multiple_queries.mdx
docs/how_to/multimodal_prompts.md
docs/how_to/multimodal_prompts.mdx
docs/how_to/multimodal_inputs.md
docs/how_to/multimodal_inputs.mdx
docs/how_to/migrate_agent.md
docs/how_to/migrate_agent.mdx
docs/how_to/logprobs.md
Expand Down Expand Up @@ -135,6 +137,10 @@ docs/how_to/extraction_examples.md
docs/how_to/extraction_examples.mdx
docs/how_to/example_selectors.md
docs/how_to/example_selectors.mdx
docs/how_to/document_loader_markdown.md
docs/how_to/document_loader_markdown.mdx
docs/how_to/document_loader_html.md
docs/how_to/document_loader_html.mdx
docs/how_to/custom_tools.md
docs/how_to/custom_tools.mdx
docs/how_to/custom_callbacks.md
Expand Down Expand Up @@ -164,4 +170,6 @@ docs/how_to/binding.mdx
docs/how_to/assign.md
docs/how_to/assign.mdx
docs/how_to/agent_executor.md
docs/how_to/agent_executor.mdx
docs/how_to/agent_executor.mdx
docs/integrations/llms/mistral.md
docs/integrations/llms/mistral.mdx
51 changes: 43 additions & 8 deletions docs/core_docs/docs/how_to/chat_token_usage_tracking.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ This guide assumes familiarity with the following concepts:

This notebook goes over how to track your token usage for specific calls.

## Using `AIMessage.response_metadata`
## Using `AIMessage.usage_metadata`

A number of model providers return token usage information as part of the chat generation response. When available, this is included in the `AIMessage.response_metadata` field.
Here's an example with OpenAI:
A number of model providers return token usage information as part of the chat generation response. When available, this information will be included on the `AIMessage` objects produced by the corresponding model.

import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/models/chat/token_usage_tracking.ts";
LangChain `AIMessage` objects include a [`usage_metadata`](https://api.js.langchain.com/classes/langchain_core_messages.AIMessage.html#usage_metadata) attribute for supported providers. When populated, this attribute will be an object with standard keys (e.g., "input_tokens" and "output_tokens").

#### OpenAI

import CodeBlock from "@theme/CodeBlock";
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>
Expand All @@ -30,18 +31,52 @@ import IntegrationInstallTooltip from "@mdx_components/integration_install_toolt
npm install @langchain/openai
```

<CodeBlock language="typescript">{Example}</CodeBlock>
import UsageMetadataExample from "@examples/models/chat/usage_metadata.ts";

And here's an example with Anthropic:
<CodeBlock language="typescript">{UsageMetadataExample}</CodeBlock>

import AnthropicExample from "@examples/models/chat/token_usage_tracking_anthropic.ts";
#### Anthropic

```bash npm2yarn
npm install @langchain/anthropic
```

import UsageMetadataExampleAnthropic from "@examples/models/chat/usage_metadata_anthropic.ts";

<CodeBlock language="typescript">{UsageMetadataExampleAnthropic}</CodeBlock>

## Using `AIMessage.response_metadata`

A number of model providers return token usage information as part of the chat generation response. When available, this is included in the `AIMessage.response_metadata` field.

#### OpenAI

import Example from "@examples/models/chat/token_usage_tracking.ts";

<CodeBlock language="typescript">{Example}</CodeBlock>

#### Anthropic

import AnthropicExample from "@examples/models/chat/token_usage_tracking_anthropic.ts";

<CodeBlock language="typescript">{AnthropicExample}</CodeBlock>

## Streaming

Some providers support token count metadata in a streaming context.

#### OpenAI

For example, OpenAI will return a message chunk at the end of a stream with token usage information. This behavior is supported by `@langchain/openai` >= 0.1.0 and can be enabled by passing a `stream_options` parameter when making your call.

:::info
By default, the last message chunk in a stream will include a `finish_reason` in the message's `response_metadata` attribute. If we include token usage in streaming mode, an additional chunk containing usage metadata will be added to the end of the stream, such that `finish_reason` appears on the second to last message chunk.
:::

import OpenAIStreamTokens from "@examples/models/chat/integration_openai_stream_tokens.ts";

<CodeBlock language="typescript">{OpenAIStreamTokens}</CodeBlock>

## Using callbacks

You can also use the `handleLLMEnd` callback to get the full output from the LLM, including token usage for supported models.
Expand Down
1 change: 1 addition & 0 deletions docs/core_docs/docs/how_to/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ All of LangChain components can easily be extended to support your own versions.

- [How to: pass multimodal data directly to models](/docs/how_to/multimodal_inputs/)
- [How to: use multimodal prompts](/docs/how_to/multimodal_prompts/)
- [How to: call tools with multimodal data](/docs/how_to/tool_calls_multimodal/)

## Use cases

Expand Down
Loading
Loading