Skip to main content
The simplest way to add Literal AI to your codebase is to use the provided wrappers. Wrappers are based on the AsyncLocalStorage API and are designed to play nice with async code. Wrappers will automatically handle the thread and step IDs for you, and will also automatically send entities to the Literal AI API upon exiting. When a step wrapper is exited, the following actions will be taken :
  • Start and end time are logged
  • The output from the wrapped function is logged as the output of the step
  • The updated step is sent to the Literal AI API
If your environment doesn’t support AsyncLocalStorage, or if you don’t like the callback style, you can always revert to the wrapper-less syntax. Please note that this syntax requires you to manually manage thread and step IDs, and also to manually send entities to the Literal AI API.

Basic usage

Thread wrapper

Step wrapper

Decoration wrapper

This wrapper is used to add metadata and tags to everything that will be logged inside it. It can also be used to specify in advance the ID of any generation created inside it.
This is especially useful when using one of our integrations, as you may not be able to specify metadata or tags directly in the API call, and you may not be able to retrieve the ID of the generation that was logged.

Advanced usage

Nesting wrappers

One advantage of using wrappers is that the structure of your code will closely represent the structure of the conversation as it is logged on Literal AI. For example consider the following code :
This will result in the following structure on Literal AI :
Step with score

Nested threads and steps

Wrapping existing threads or steps

When you fetch a thread or step from the client, you can wrap it with the wrap method. This will allow you to add additional steps or threads to the existing one.

Updating steps and threads

Using the context

You can also update the current step or thread using the context object. The context object is available in the wrapped function and it will be sent to Literal AI upon exiting the wrapped function. There are two ways to access the context object:
  • It is provided as an argument to the wrapper function
  • It is available as a property of the client object : client.getCurrentThread() and client.getCurrentStep()
The getCurrentThread and getCurrentStep methods are type-safe and they will throw an error if called outside of a wrapped context.

Using the wrapper’s callback

The wrappers take a second argument that allows you to update the current step or thread upon exiting the wrapped function. The second argument can either be a static object, or a function that takes the wrapper’s output as an argument and returns a valid step or thread object.