You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/langsmith/annotate-code.mdx
+19-15Lines changed: 19 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,32 +1,36 @@
1
1
---
2
2
title: Custom instrumentation
3
3
sidebarTitle: Customize instrumentation
4
+
description: Instrument your code directly to control which functions are traced and how they appear in LangSmith.
4
5
---
5
6
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:
7
8
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).
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>
11
18
12
19
## Prerequisites
13
20
14
21
Before tracing, set the following environment variables:
15
22
16
23
-`LANGSMITH_TRACING=true`: enables tracing. Set this to toggle tracing on and off without changing your code.
17
24
-`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).
18
26
19
27
<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.
21
29
</Note>
22
30
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
-
27
31
## Use `@traceable` / `traceable`
28
32
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.
30
34
31
35
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.
32
36
@@ -123,7 +127,7 @@ await runPipeline();
123
127
124
128
</CodeGroup>
125
129
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.
127
131
128
132
<Note>
129
133
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
140
144
141
145
The context manager integrates seamlessly with the `traceable` decorator and `wrap_openai` wrapper, so you can use them together in the same application.
142
146
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 decorated—instead, `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()`:
144
148
145
149
```python
146
150
import openai
@@ -173,9 +177,9 @@ with ls.trace("Chat Pipeline", "chain", project_name="my_test", inputs=app_input
173
177
174
178
## Use the `RunTree` API
175
179
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.
177
181
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.
179
183
180
184
<CodeGroup>
181
185
@@ -366,6 +370,6 @@ try {
366
370
367
371
### Use LangChain
368
372
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).
370
374
371
375
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