BaseLiteralClient

class BaseLiteralClient()

Base class for LiteralClient and AsyncLiteralClient.

Example:

from literalai import LiteralClient, AsyncLiteralClient

# Initialize the client
client = LiteralClient(api_key="your_api_key_here")
async_client = AsyncLiteralClient(api_key="your_api_key_here")

Attributes:

api
Union[LiteralAPI, AsyncLiteralAPI]
The API client used for communication with Literal AI.
disabled
bool
Flag indicating whether the client is disabled.
event_processor
EventProcessor
Processor for handling events.

instrument_openai

def instrument_openai()

Instruments the OpenAI SDK so that all LLM calls are logged to Literal AI.

instrument_mistralai

def instrument_mistralai()

Instruments the Mistral AI SDK so that all LLM calls are logged to Literal AI.

instrument_llamaindex

def instrument_llamaindex()

Instruments the Llama Index framework so that all RAG & LLM calls are logged to Literal AI.

langchain_callback

def langchain_callback(to_ignore: Optional[List[str]] = None,
                       to_keep: Optional[List[str]] = None,
                       **kwargs: Any)

Creates a Callback for Langchain that logs all LLM calls to Literal AI.

Arguments:

to_ignore
Optional[List[str]]
Runs to ignore to declutter logging.
to_keep
Optional[List[str]]
Runs to keep within ignored runs.

Returns:

LangchainTracer
The callback to use in Langchain’s invoke methods.

start_step

def start_step(name: str = "",
               type: Optional[TrueStepType] = None,
               id: Optional[str] = None,
               parent_id: Optional[str] = None,
               thread_id: Optional[str] = None,
               root_run_id: Optional[str] = None,
               **kwargs)

Creates a step and starts it in the current context. To log it on Literal AI use .end(). This is used to create Agent steps. For conversational messages use message instead.

Arguments:

name
Optional[str]
The name of the step to create.
type
TrueStepType
The type of the step. Must be one of the following :
“run”, “tool”, “llm”, “embedding”, “retrieval”,“rerank”, “undefined”.
id
Optional[str]
The id of the step to create.
parent_id
Optional[str]
The id of the parent step.
thread_id
Optional[str]
The id of the parent thread.
root_run_id
Optional[str]
The id of the root run.

Returns:

Step
the created step.

get_current_step

def get_current_step()

Gets the current step from the context.

get_current_thread

def get_current_thread()

Gets the current thread from the context.

get_current_root_run

def get_current_root_run()

Gets the current root run from the context.

reset_context

def reset_context()

Resets the context, forgetting active steps & setting current thread to None.

flush_and_stop

def flush_and_stop()

Sends all threads and steps to the Literal AI API. Waits synchronously for all API calls to be done.

LiteralClient

class LiteralClient(BaseLiteralClient)

Synchronous client for interacting with the Literal AI API.

Example:

from literalai import LiteralClient
# Initialize the client
client = LiteralClient(api_key="your_api_key_here")

AsyncLiteralClient

class AsyncLiteralClient(BaseLiteralClient)

Asynchronous client for interacting with the Literal AI API.

Example:

from literalai import AsyncLiteralClient
# Initialize the client
async_client = AsyncLiteralClient(api_key="your_api_key_here")