How to log AI workflow execution
Quick answer
Use Python's
logging module to capture AI workflow execution details such as inputs, outputs, timestamps, and errors. Integrate logging calls around your OpenAI SDK calls to record each step of the AI interaction for traceability and debugging.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup logging in Python
Configure Python's built-in logging module to output logs to console or file with timestamps and log levels. This setup is essential for capturing AI workflow events.
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler()]
)
logger = logging.getLogger(__name__)
logger.info("Logging is configured.") output
2026-04-27 12:00:00,000 - INFO - Logging is configured.
Step by step AI workflow logging
Wrap your AI calls with logging statements to record inputs, outputs, and errors. This example uses the openai SDK v1+ pattern with OpenAI client and gpt-4o model.
import os
from openai import OpenAI
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
try:
user_input = "Explain AI workflow logging"
logger.info(f"User input: {user_input}")
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": user_input}]
)
output_text = response.choices[0].message.content
logger.info(f"AI output: {output_text}")
except Exception as e:
logger.error(f"Error during AI call: {e}")
raise output
2026-04-27 12:00:01,000 - INFO - User input: Explain AI workflow logging 2026-04-27 12:00:02,500 - INFO - AI output: AI workflow logging involves capturing inputs, outputs, timestamps, and errors to monitor and debug AI applications.
Common variations
You can extend logging to async workflows, streaming responses, or use other AI SDKs like Anthropic or Google Vertex AI. For async, use asyncio and async for with logging inside the loop. For streaming, log each chunk received.
import os
import asyncio
from openai import OpenAI
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
async def async_stream_log():
user_input = "Stream AI response with logging"
logger.info(f"User input: {user_input}")
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": user_input}],
stream=True
)
collected = []
async for chunk in stream:
delta = chunk.choices[0].delta.content or ""
logger.info(f"Received chunk: {delta}")
collected.append(delta)
full_response = ''.join(collected)
logger.info(f"Full streamed response: {full_response}")
asyncio.run(async_stream_log()) output
2026-04-27 12:00:03,000 - INFO - User input: Stream AI response with logging 2026-04-27 12:00:03,100 - INFO - Received chunk: AI 2026-04-27 12:00:03,200 - INFO - Received chunk: workflow 2026-04-27 12:00:03,300 - INFO - Received chunk: logging involves capturing data in real-time. 2026-04-27 12:00:03,400 - INFO - Full streamed response: AI workflow logging involves capturing data in real-time.
Troubleshooting logging issues
- If logs do not appear, ensure
logging.basicConfigis called before any logging. - Check environment variable
OPENAI_API_KEYis set correctly. - For async logging, confirm your event loop is running and
awaitis used properly. - Use
logger.errorto capture exceptions and stack traces for debugging.
Key Takeaways
- Use Python's built-in
loggingmodule to capture AI workflow inputs, outputs, and errors. - Wrap AI SDK calls with logging statements for traceability and debugging.
- For streaming or async workflows, log each chunk or event as it arrives.
- Always configure logging before running AI calls to ensure logs are captured.
- Check environment variables and exception logs to troubleshoot issues.