Chaining calls (Sessions)
This guide explains how to use sequences of LLM calls to achieve a task in Humanloop. Humanloop allows you to trace through “sessions”, enabling you to track sequences of actions taken by your LLM agent and troubleshoot where your LLM chain went wrong.
This guide contains 3 sections. We’ll start with an example Python script that makes a series of calls to an LLM upon receiving a user request. In the first section, we’ll log these calls to Humanloop. In the second section, we’ll link up these calls to a single session so they can be easily inspected on Humanloop. Finally, we’ll explore how to deal with nested logs within a session.
By following this guide, you will:
- Have hooked up your backend system to use Humanloop.
- Be able to view session traces displaying sequences of LLM calls on Humanloop.
- Learn how to log complex session traces containing nested logs.
Prerequisites
- A Humanloop account. If you don’t have one, you can create an account now by going to the Sign up page.
- You have a system making a series of LLM calls when a user makes a request. If you do not have one, you can use the following example Python script. In this guide, we’ll be illustrating the steps to be taken with specific modifications to this script.
If you don’t use Python, you can checkout our TypeScript SDK or the underlying API in our Postman collection for the corresponding endpoints.
Example script
To set up your local environment to run this script, you will need to have installed Python 3 and the following libraries:
pip install openai google-search-results
.
Send logs to Humanloop
To send logs to Humanloop, we’ll install and use the Humanloop Python SDK.
Initialize the Humanloop client:
Add the following lines to the top of the example file. (Get your API key from your Organisation Settings page)
Use Humanloop to fetch the moderator response. This automatically sends the logs to Humanloop:
Replace your openai.ChatCompletion.create()
call under # Check for abuse
with a humanloop.chat()
call.
Instead of replacing your model call with humanloop.chat()
you can
alternatively add a humanloop.log()
call after your model call. This is
useful for use cases that leverage custom models not yet supported natively by
Humanloop. See our Using your own model guide
for more information.
You have now connected your multiple calls to Humanloop, logging them to individual projects. While each one can be inspected individually, we can’t yet view them together to evaluate and improve our pipeline.
Post logs to a session
To view the logs for a single user_request
together, we can log them to a session. This requires a simple change of just passing in the same session id to the different calls.
Final example script
This is the updated version of the example script above with Humanloop fully integrated. Running this script yields sessions that can be inspected on Humanloop.
Nesting logs within a session [Extension]
A more complicated trace involving nested logs, such as those recording an Agent’s behaviour, can also be logged and viewed in Humanloop.
First, post a log to a session, specifying both session_reference_id
and reference_id
. Then, pass in this reference_id
as parent_reference_id
in a subsequent log request. This indicates to Humanloop that this second log should be nested under the first.
Deferred output population
In most cases, you don’t know the output for a parent log until all of its children have completed. For instance, the root-level Agent will spin off multiple LLM requests before it can retrieve an output. To support this case, we allow logging without an output. The output can then be updated after the session is complete with a separate humanloop.logs_api.update_by_reference_id(reference_id, output)
call.