Skip to content

Commit 420babb

Browse files
authored
Style changes / updates to custom instrumentation page (#3560)
1 parent 3db5f87 commit 420babb

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/langsmith/annotate-code.mdx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
---
22
title: Custom instrumentation
33
sidebarTitle: Customize instrumentation
4+
description: Instrument your code directly to control which functions are traced and how they appear in LangSmith.
45
---
56

6-
LangSmith integrations handle tracing automatically. Custom instrumentation lets you define exactly which functions are traced, control what inputs and outputs are logged, and structure your trace hierarchy without rewriting your application logic.
7+
Adding instrumentation directly to your code gives you precise control over which functions your application traces, what inputs and outputs are logged, and how your [trace](/langsmith/observability-concepts#traces) hierarchy is structured. This page covers three approaches:
78

8-
<Check>
9-
If you are using LangChain (either Python or JS/TS), go directly to the [LangChain-specific instructions](/langsmith/trace-with-langchain).
10-
</Check>
9+
- [`@traceable` decorator](#use-%40traceable-%2F-traceable): recommended for most cases
10+
- [`trace` context manager](#use-the-trace-context-manager-python-only): Python only
11+
- [`RunTree` API](#use-the-runtree-api): explicit, low-level control
12+
13+
For LangChain (Python or JS/TS), refer to the [LangChain-specific instructions](/langsmith/trace-with-langchain).
14+
15+
<Callout icon="plug" color="#4F46E5" iconType="regular">
16+
If you're using an LLM provider or agent framework with a built-in LangSmith integration, refer to the [integrations overview](/langsmith/integrations) instead
17+
</Callout>
1118

1219
## Prerequisites
1320

1421
Before tracing, set the following environment variables:
1522

1623
- `LANGSMITH_TRACING=true`: enables tracing. Set this to toggle tracing on and off without changing your code.
1724
- `LANGSMITH_API_KEY`: your [LangSmith API key](/langsmith/create-account-api-key).
25+
- By default, LangSmith logs traces to a project named `default`. To log to a different project, set `LANGSMITH_PROJECT`. For more details, refer to [Log traces to a specific project](/langsmith/log-traces-to-project).
1826

1927
<Note>
20-
To disable tracing, remove the `LANGSMITH_TRACING` environment variable. This does not affect `RunTree` objects or direct API usage, which are low-level and not controlled by the tracing toggle.
28+
`LANGSMITH_TRACING` controls the `@traceable` decorator and the `trace` context manager. To override this at runtime for `@traceable` without changing environment variables, use `tracing_context(enabled=True/False)` (Python) or pass `tracingEnabled` directly to `traceable` (JS/TS). `RunTree` objects are not affected by any of these controls; they always send data to LangSmith when posted.
2129
</Note>
2230

23-
<Tip>
24-
By default, traces are logged to a project named `default`. To log to a different project, see [Log traces to a specific project](/langsmith/log-traces-to-project).
25-
</Tip>
26-
2731
## Use `@traceable` / `traceable`
2832

29-
The recommended approach is the @[`@traceable`] decorator (Python) or [`traceable`](https://reference.langchain.com/javascript/langsmith/traceable) wrapper (TypeScript). Apply it to any function to make it a traced run, and LangSmith handles context propagation across nested calls automatically.
33+
Apply @[`@traceable`] (Python) or [`traceable`](https://reference.langchain.com/javascript/langsmith/traceable) (TypeScript) to any function to make it a traced run. LangSmith handles context propagation across nested calls automatically.
3034

3135
The following example traces a simple pipeline: `run_pipeline` calls `format_prompt` to build the messages, `invoke_llm` to call the model, and `parse_output` to extract the result.
3236

@@ -123,7 +127,7 @@ await runPipeline();
123127

124128
</CodeGroup>
125129

126-
In LangSmith, you'll see a `run_pipeline` trace with `format_prompt`, `invoke_llm`, and `parse_output` as nested child runs.
130+
In the [UI](https://smith.langchain.com), you'll find a `run_pipeline` trace with `format_prompt`, `invoke_llm`, and `parse_output` as nested child runs.
127131

128132
<Note>
129133
When you wrap a sync function with `traceable` (e.g., `formatPrompt` in the previous example), use the `await` keyword when calling it to ensure the trace is logged correctly.
@@ -140,7 +144,7 @@ In Python, you can use the `trace` context manager to log traces to LangSmith. T
140144

141145
The context manager integrates seamlessly with the `traceable` decorator and `wrap_openai` wrapper, so you can use them together in the same application.
142146

143-
The following example shows all three used together. `wrap_openai` wraps the OpenAI client so its calls are traced automatically. `my_tool` uses `@traceable` with `run_type="tool"` and a custom `name` to appear correctly in the trace. `chat_pipeline` itself is not decoratedinstead, `ls.trace` wraps the call, letting you pass the project name and inputs explicitly and set outputs manually via `rt.end()`:
147+
The following example shows all three used together. `wrap_openai` wraps the OpenAI client so its calls are traced automatically. `my_tool` uses `@traceable` with `run_type="tool"` and a custom `name` to appear correctly in the trace. `chat_pipeline` itself is not decorated; instead, `ls.trace` wraps the call, letting you pass the project name and inputs explicitly and set outputs manually via `rt.end()`:
144148

145149
```python
146150
import openai
@@ -173,9 +177,9 @@ with ls.trace("Chat Pipeline", "chain", project_name="my_test", inputs=app_input
173177

174178
## Use the `RunTree` API
175179

176-
Another, more explicit way to log traces to LangSmith is via the `RunTree` API. This API allows you more control over your tracing - you can manually create runs and children runs to assemble your trace. You still need to set your `LANGSMITH_API_KEY`, but `LANGSMITH_TRACING` is not necessary for this method.
180+
Another, more explicit way to log traces to LangSmith is via the `RunTree` API. This API allows you more control over your tracing. You can manually create runs and children runs to assemble your trace. You still need to set your `LANGSMITH_API_KEY`, but `LANGSMITH_TRACING` is not necessary for this method.
177181

178-
This method is not recommended, as it's easier to make mistakes in propagating trace context.
182+
This method is not recommended for most use cases; manually managing trace context is error-prone compared to `@traceable`, which handles context propagation automatically.
179183

180184
<CodeGroup>
181185

@@ -366,6 +370,6 @@ try {
366370

367371
### Use LangChain
368372

369-
If you are using LangChain, please refer to our [LangChain tracing guide](/langsmith/trace-with-langchain#ensure-all-traces-are-submitted-before-exiting).
373+
If you are using LangChain, refer to the [LangChain tracing guide](/langsmith/trace-with-langchain#ensure-all-traces-are-submitted-before-exiting).
370374

371375
If you prefer a video tutorial, check out the [Tracing Basics video](https://academy.langchain.com/pages/intro-to-langsmith-preview) from the Introduction to LangSmith Course.

0 commit comments

Comments
 (0)