Skip to Content

Replicate (tracing)

Replicate  is a platform for running machine learning models in the cloud. It hosts thousands of open-source models and provides a simple API for running predictions without managing infrastructure.

Set up Respan

Create an account at platform.respan.ai  and grab an API key .

Paste this into your coding agent:

Help me set up Respan tracing with the Replicate integration. Follow https://www.respan.ai/docs/integrations/replicate.md

Setup

Install packages

Terminal
pip install respan-ai respan-instrumentation-replicate replicate

Set environment variables

Terminal
export REPLICATE_API_TOKEN="YOUR_REPLICATE_API_TOKEN" export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

REPLICATE_API_TOKEN is used for Replicate predictions. RESPAN_API_KEY is used to export traces to Respan.

Initialize and run

Python
import os import replicate from respan import Respan, workflow from respan_instrumentation_replicate import ReplicateInstrumentor respan = Respan( api_key=os.environ["RESPAN_API_KEY"], instrumentations=[ReplicateInstrumentor()], ) @workflow(name="replicate_quickstart.workflow") def run_prediction() -> str: output = replicate.run( "meta/meta-llama-3-8b-instruct", input={"prompt": "Reply with one concise sentence about tracing."}, ) return "".join(str(chunk) for chunk in output) if not isinstance(output, str) else output print(run_prediction()) respan.shutdown()

View your trace

Open the Traces page  and search for workflow name replicate_quickstart.workflow.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. ReplicateInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Python
from respan import Respan from respan_instrumentation_replicate import ReplicateInstrumentor respan = Respan( instrumentations=[ReplicateInstrumentor()], customer_identifier="user_123", metadata={"service": "replicate-api", "version": "1.0.0"}, )

With propagate_attributes

Python
import replicate from respan import Respan, propagate_attributes from respan_instrumentation_replicate import ReplicateInstrumentor respan = Respan(instrumentations=[ReplicateInstrumentor()]) def handle_request(user_id: str, prompt: str): with propagate_attributes( customer_identifier=user_id, thread_identifier="conv_abc_123", metadata={"plan": "pro"}, ): output = replicate.run("meta/meta-llama-3-8b-instruct", input={"prompt": prompt}) print("".join(str(chunk) for chunk in output) if not isinstance(output, str) else output)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.