import langsmith as lsfrom langsmith import traceable@traceabledef my_function(input_text: str): return process(input_text)def client_requires_zero_retention(client_id: str) -> bool: """ Check if a client has a zero-retention policy. In production, this would query a database, configuration service, or feature flag system. Consider caching results for performance. """ # Example: Query from database or config zero_retention_clients = get_zero_retention_clients() # Your implementation return client_id in zero_retention_clientsdef handle_request(client_id: str, user_input: str): """ Process a request with conditional tracing based on client requirements. """ should_disable = client_requires_zero_retention(client_id) with ls.tracing_context(enabled=not should_disable): return my_function(user_input)# Example usagehandle_request("client-a", "some input") # Traced or not based on client settings
import contextlibimport langsmith as lsfrom langgraph_sdk.runtime import ServerRuntime@contextlib.asynccontextmanagerasync def make_graph(runtime: ServerRuntime): graph = build_my_graph() # You can use tracing_context to dynamically enable/disable tracing, # set metadata or tags, override the tracing project, etc. with ls.tracing_context(enabled=False, metadata={"foo": "bar"}): yield graph
import { traceable } from "langsmith/traceable";// Define the core logic oncefunction processText(inputText: string): string { // Your actual processing logic return inputText.toUpperCase();}// Create traced and non-traced variants upfrontconst myFunction = traceable(processText, { name: "my_function" });const myFunctionNoTrace = traceable(processText, { name: "my_function", tracingEnabled: false});function clientRequiresZeroRetention(clientId: string): boolean { /** * Check if a client has a zero-retention policy. * * In production, this would query a database, configuration service, * or feature flag system. Consider caching results for performance. */ const zeroRetentionClients = getZeroRetentionClients(); // Your implementation return zeroRetentionClients.includes(clientId);}async function handleRequest(clientId: string, userInput: string) { /** * Process a request with conditional tracing based on client requirements. * Efficiently selects pre-created traced or non-traced variant. */ const shouldDisable = clientRequiresZeroRetention(clientId); // Select the appropriate pre-created variant const fn = shouldDisable ? myFunctionNoTrace : myFunction; return await fn(userInput);}// Example usageawait handleRequest("client-a", "some input"); // Traced or not based on client settings