ProvidersMorePortkeyLog, Trace, and MonitorOn this pageLog, Trace, and MonitorWhen building apps or agents using Langchain, you end up making multiple API calls to fulfill a single user request. However, these requests are not chained when you want to analyse them. With Portkey, all the embeddings, completion, and other requests from a single user request will get logged and traced to a common ID, enabling you to gain full visibility of user interactions.This notebook serves as a step-by-step guide on how to log, trace, and monitor Langchain LLM calls using Portkey in your Langchain app.First, let's import Portkey, OpenAI, and Agent toolsimport osfrom langchain.agents import AgentType, initialize_agent, load_toolsfrom langchain.llms import OpenAIfrom langchain.utilities import PortkeyPaste your OpenAI API key below. (You can find it here)os.environ["OPENAI_API_KEY"] = "<OPENAI_API_KEY>"Get Portkey API Key​Sign up for Portkey hereOn your dashboard, click on the profile icon on the top left, then click on "Copy API Key"Paste it belowPORTKEY_API_KEY = "<PORTKEY_API_KEY>"  # Paste your Portkey API Key hereSet Trace ID​Set the trace id for your request belowThe Trace ID can be common for all API calls originating from a single requestTRACE_ID = "portkey_langchain_demo"  # Set trace id hereGenerate Portkey Headers​headers = Portkey.Config(    api_key=PORTKEY_API_KEY,    trace_id=TRACE_ID,)Run your agent as usual. The only change is that we will include the above headers in the request now.llm = OpenAI(temperature=0, headers=headers)tools = load_tools(["serpapi", "llm-math"], llm=llm)agent = initialize_agent(    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)# Let's test it out!agent.run(    "What was the high temperature in SF yesterday in Fahrenheit? What is that number raised to the .023 power?")How Logging & Tracing Works on Portkey​LoggingSending your request through Portkey ensures that all of the requests are logged by defaultEach request log contains timestamp, model name, total cost, request time, request json, response json, and additional Portkey featuresTracingTrace id is passed along with each request and is visibe on the logs on Portkey dashboardYou can also set a distinct trace id for each request if you wantYou can append user feedback to a trace id as well. More info on this hereAdvanced LLMOps Features - Caching, Tagging, Retries​In addition to logging and tracing, Portkey provides more features that add production capabilities to your existing workflows:CachingRespond to previously served customers queries from cache instead of sending them again to OpenAI. Match exact strings OR semantically similar strings. Cache can save costs and reduce latencies by 20x.RetriesAutomatically reprocess any unsuccessful API requests upto 5 times. Uses an exponential backoff strategy, which spaces out retry attempts to prevent network overload.FeatureConfig KeyValue (Type)🔁 Automatic Retriesretry_countinteger [1,2,3,4,5]🧠 Enabling Cachecachesimple OR semanticTaggingTrack and audit ach user interaction in high detail with predefined tags.TagConfig KeyValue (Type)User TaguserstringOrganisation TagorganisationstringEnvironment TagenvironmentstringPrompt Tag (version/id/string)promptstringCode Example With All Features​headers = Portkey.Config(    # Mandatory    api_key="<PORTKEY_API_KEY>",    # Cache Options    cache="semantic",    cache_force_refresh="True",    cache_age=1729,    # Advanced    retry_count=5,    trace_id="langchain_agent",    # Metadata    environment="production",    user="john",    organisation="acme",    prompt="Frost",)llm = OpenAI(temperature=0.9, headers=headers)print(llm("Two roads diverged in the yellow woods"))PreviousPortkeyNextPredibaseGet Portkey API KeySet Trace IDGenerate Portkey HeadersHow Logging & Tracing Works on PortkeyAdvanced LLMOps Features - Caching, Tagging, RetriesCode Example With All Features