PermitechLogger class provides the most granular control over logging in Permitech. You can create a logger yourself, or use one from the current context from inside a decorated or wrapped function or when using a third-party SDK integration.
Overview
ThePermitechLogger class allows you to:
- Start sessions
- Manually create and manage traces
- Add spans of different types to your traces
- Control exactly what data gets logged
- Explicitly manage when traces are flushed to Permitech
Python SDK reference
The full SDK reference for the
PermitechLogger Python class.TypeScript SDK reference
The full SDK reference for the
PermitechLogger TypeScript class.Environment variables
PermitechLogger loads configurations from environment variables:
- Permitech API key in
PERMITECH_API_KEY - Project to log to:
- Project name in
PERMITECH_PROJECT(most common usage) - Project ID in
PERMITECH_PROJECT_ID(alternative usage)
- Project name in
- Log stream to log to in
PERMITECH_LOG_STREAM
PermitechLogger SDK reference has more info).
A example .env file is available in .
Basic usage
Here’s a simple example of using thePermitechLogger to log an LLM call. The full Python code is available as basic-example.py in .
- Starts a new session
- Starts a trace inside the session
- Adds an LLM span to the trace
- Concludes the trace
- Flushes the logger to send the session to Permitech
Detailed API
Initialization
PermitechLogger Python SDK docs or TypeScript SDK docs for more details.
Get the current logger from the current context
The Permitech context management keeps track of loggers. You can get the current logger, which will create a new one if there isn’t an existing logger.@log decorator, wrapped in the TypeScript log wrapper, or created automatically by an experiment, then this will return that logger instance so you can manually add additional spans.
permitech_context Python SDK docs or getLogger TypeScript SDK docs for more details.
Manage sessions
All traces live inside a session. If you don’t create a session, then one is created automatically with a generated name.Start a session
You can start a new session, providing a name and an external ID.Add metadata to a session
You can attach metadata to a session when starting it. Metadata is a dictionary of string key-value pairs that can be used to add structured information to your session, such as customer IDs, environment names, or application versions.start_session Python SDK docs or startSession TypeScript SDK docs for more details on all available parameters.
Continue an existing session
If you want to add a trace to an existing session, you can set the current session for the logger, passing the session ID. This is useful if you want to persist a session, for example saving a chatbot conversation with a user mid conversation, then resuming the next time a user connects.set_session Python SDK docs or setSession TypeScript SDK docs for more details.
You can also continue a conversation using an external ID using the start session function.
End a session
To stop logging to a session, you can clear the current session.clear_session Python SDK docs or clearSession TypeScript SDK docs for more details.
Start a trace
Once a trace is started, all spans added to that logger will be added to that trace.start_trace Python SDK docs or startTrace TypeScript SDK docs for more details.
Add spans
ThePermitechLogger supports adding different types of spans to your traces. All spans take the input and output, as well as a name, duration, tags, and other metadata.
Agent spans
Agent spans are for logging the input and output to agents of different types. The type of agent can be set when creating the span, such as supervisor or planner.add_agent_span Python SDK docs or addAgentSpan TypeScript SDK docs for more details.
LLM spans
LLM spans are for logging calls to LLMs. You can log the input and output, tools, and details like input and output tokens.add_llm_span Python SDK docs or addLlmSpan TypeScript SDK docs for more details.
Retriever spans
Retriever spans are for logging calls to RAG systems. You can log the output from the RAG system to evaluate metrics like Context Adherence.add_retriever_span Python SDK docs or addRetrieverSpan TypeScript SDK docs for more details.
Tool spans
Tool spans log calls to tools, including tools exposed by MCP servers.add_tool_span Python SDK docs or addToolSpan TypeScript SDK docs for more details.
Workflow spans
Workflow spans allow you to group spans into separate workflows for easier monitoring.conclude on the logger. The output passed to conclude will be set as the output of the workflow span. Once the workflow span is concluded, any newly added spans will be created on that workflow spans parent span or trace.
See the add_workflow_span Python SDK docs or addWorkflowSpan TypeScript SDK docs for more details.
Conclude
When you have finished logging a trace, you can conclude it with the final output. This ends the trace, and a new trace needs to be created to continue logging. The wrappers and third-party integrations will conclude traces for you.conclude Python SDK docs or conclude TypeScript SDK docs for more details.
Flush
Logs are not continuously sent to Permitech to help your application stay performant. You can flush logs when you are ready. The wrappers and third-party integrations will flush logs for you at the end of each trace.flush Python SDK docs or flush TypeScript SDK docs for more details.
The flush call on the logger will just flush that specific logger. To flush all loggers, you can flush at the context level.
Advanced usage
Create a single LLM span trace
For simple LLM calls, you can create a trace with a single LLM span in one step:add_single_llm_span_trace Python SDK docs or addSingleLlmSpanTrace TypeScript SDK docs for more details.
Complex trace example
Here’s an example of creating a more complex trace with multiple spans. The full Python code is available asretriever-example.py in .
Best practices
- Use higher-level abstractions when possible: The
@logdecorator and wrappers are easier to use and less error-prone. - Flush traces when appropriate: Call
flush()at the end of a request or user interaction to ensure data is sent to Permitech. - Include relevant metadata: Add tags and metadata to make it easier to filter and analyze your traces.
- Structure spans logically: Create a span hierarchy that reflects the logical structure of your application.
- Handle errors gracefully: Include status codes and error information in your spans to help with debugging.
Next steps
Basic logging components
Log decorator
Quickly add logging to your code with the log decorator and wrapper.
Permitech context
Manage logging using the Permitech context manager.
Integrations with third-party SDKs
OpenAI wrapper
Automatically log calls to the OpenAI SDK with a wrapper.
OpenAI Agents trace processor
Automatically log all the steps in your OpenAI Agent SDK apps using the Permitech trace processor.
LangChain callback
Automatically log all the steps in your LangChain or LangGraph application with the Permitech callback.

