Nothing will break if you don’t log LLM traces in the correct format - data will still be logged. However, the data will not be processed or rendered in a way that is specific to LLMs.
Chat-style models
Using LangChain OSS or LangSmith wrappers
If you are using LangChain OSS or LangSmith wrappers, you don’t need to do anything special. The wrappers will automatically log traces in the correct format.Implementing your own custom chat-model
If you are implementing your own custom chat-model, you need to ensure that your inputs contain a keymessages
with a list of dictionaries/objects. Each dictionary/object must contain the keys role
and content
with string values. The output must return an object that, when serialized, contains the key choices
with a list of dictionaries/objects. Each must contain the key message
with a dictionary/object that contains the keys role
and content
with string values.
To make your custom LLM traces appear well-formatted in the LangSmith UI, your trace inputs and outputs must conform to a format LangSmith recognizes:
-
A list of messages in OpenAI or Anthropic format, represented as Python dictionaries or TypeScript objects.
- Each message must contain the key
role
andcontent
. - Messages with the
"assistant"
role may optionally containtool_calls
. Thesetool_calls
may be in OpenAI format or LangChain’s format.
- Each message must contain the key
-
An dict/object containing
"messages"
key with a list of messages in the above format.- LangSmith may use additional parameters in this input dict that match OpenAI’s chat completion endpoint for rendering in the trace view, such as a list of available
tools
for the model to call.
- LangSmith may use additional parameters in this input dict that match OpenAI’s chat completion endpoint for rendering in the trace view, such as a list of available
- List of messages
- Messages dict
- A dictionary/object that contains the key
choices
with a value that is a list of dictionaries/objects. Each dictionary/object must contain the keymessage
, which maps to a message object with the keysrole
andcontent
. - A dictionary/object that contains the key
message
with a value that is a message object with the keysrole
andcontent
. - A tuple/array of two elements, where the first element is the role and the second element is the content.
- A dictionary/object that contains the key
role
andcontent
.
- Choices format
- Message format
- Tuple format
- Direct format
metadata
fields to help LangSmith identify the model - which if recognized, LangSmith will use to automatically calculate costs. To learn more about how to use the metadata
fields, see this guide.
ls_provider
: The provider of the model, eg “openai”, “anthropic”, etc.ls_model_name
: The name of the model, eg “gpt-4o-mini”, “claude-3-opus-20240307”, etc.
- Python
- TypeScript

If
ls_model_name
is not present in extra.metadata
, other fields might be used from the extra.metadata
for estimating token counts. The following fields are used in the order of precedence:metadata.ls_model_name
inputs.model
inputs.model_name
Provide token and cost information
By default, LangSmith uses tiktoken to count tokens, utilizing a best guess at the model’s tokenizer based on thels_model_name
provided. It also calculates costs automatically by using the model pricing table. To learn how LangSmith calculates token-based costs, see this guide.
However, many models already include exact token counts as part of the response. If you have this information, you can override the default token calculation in LangSmith in one of two ways:
- Extract usage within your traced function and set a
usage_metadata
field on the run’s metadata. - Return a
usage_metadata
field in your traced function outputs.
You cannot set any fields other than the ones listed below. You do not need to include all fields.
Setting run metadata
You can modify the current run’s metadata with usage information within your traced function. The advantage of this approach is that you do not need to change your traced function’s runtime outputs. Here’s an example:Requires
langsmith>=0.3.43
(Python) and langsmith>=0.3.30
(JS/TS).- Python
- TypeScript
Setting run outputs
You can add ausage_metadata
key to the function’s response to set manual token counts and costs.
- Python
- TypeScript
Instruct-style models
For instruct-style models (string in, string out), your inputs must contain a keyprompt
with a string value. Other inputs are also permitted. The output must return an object that, when serialized, contains the key choices
with a list of dictionaries/objects. Each must contain the key text
with a string value. The same rules for metadata
and usage_metadata
apply as for chat-style models.
- Python
- TypeScript
