Skip to main content

A helper package for openAi Agents, to support implementation to AnoSys platform easly

Project description

AnoSys Logger for OpenAI Agents - Python

PyPI version License: MIT

Automatically capture and send OpenAI API calls (and more) to AnoSys for monitoring, analytics, and observability.

Features

  • Automatic Instrumentation: Automatically captures traces from OpenAI API calls via OpenTelemetry.
  • Manual Instrumentation: Easy-to-use decorator @anosys_logger for logging any function (sync or async).
  • Raw Logging: Send custom JSON data directly to AnoSys for unstructured events.
  • Async & Streaming Support: Fully supports Python's asyncio and streaming response aggregation.
  • OpenTelemetry Standards: Follows LLM / Gen AI semantic conventions.

Installation

Install the package via pip:

pip install anosys-logger-4-openai-agents

Configuration

You need an AnoSys API Key to send data. Set it as an environment variable:

export ANOSYS_API_KEY="your_anosys_api_key"

Or create a .env file in your project root:

ANOSYS_API_KEY=your_anosys_api_key

Usage

1. Automatic Tracing (with OpenAI Agents)

To automatically capture traces from OpenAI agents, register the AnosysOpenAIAgentsLogger as a trace processor. This requires the traceai-openai-agents package.

import os
import contextvars
from agents import add_trace_processor
from AnosysLoggers import AnosysOpenAIAgentsLogger

# 1. Set API Keys
os.environ["OPENAI_API_KEY"] = "your_openai_key"
os.environ['ANOSYS_API_KEY'] = "your_anosys_key"

# 2. (Optional) Setup User Context
current_user_context = contextvars.ContextVar("current_user_context")
current_user_context.set({"session_id": "session_123", "user_id": "user_456"})

# 3. Register the Logger
add_trace_processor(AnosysOpenAIAgentsLogger(get_user_context=current_user_context.get))

# Now, any agent execution will be automatically logged to AnoSys.

2. Manual Logging (Decorator)

Use the @anosys_logger decorator to log inputs, outputs, and execution details of any function.

Synchronous Example

from AnosysLoggers import anosys_logger

@anosys_logger(source="my_sync_function")
def calculate_sum(a, b):
    return a + b

# Calling the function will automatically log data to AnoSys
result = calculate_sum(5, 10)

Asynchronous Example

import asyncio
from AnosysLoggers import anosys_logger

@anosys_logger(source="my_async_function")
async def fetch_data(url):
    await asyncio.sleep(1)  # Simulate network delay
    return {"data": "sample", "url": url}

# Run the async function
asyncio.run(fetch_data("https://example.com"))

Streaming (Async Generator) Example

@anosys_logger(source="my_streaming_function")
async def stream_data():
    yield "part1"
    yield "part2"

# The logger will capture and aggregate the streamed output
async for chunk in stream_data():
    print(chunk)

3. Raw Logging

If you need to log unstructured data or custom events, use anosys_raw_logger.

from AnosysLoggers import anosys_raw_logger

data = {
    "event": "user_signup",
    "user_id": "12345",
    "status": "success"
}

anosys_raw_logger(data)

What Data is Captured?

OpenTelemetry Semantic Conventions

Following the Gen AI standards:

  • gen_ai.system: "openai"
  • gen_ai.request.model: Model requested (e.g., "gpt-4")
  • gen_ai.usage.input_tokens: Prompt token count
  • gen_ai.usage.output_tokens: Completion token count
  • gen_ai.response.finish_reasons: Why the generation stopped

Additional Metadata

  • Request/response messages (optional)
  • Execution duration and timestamps
  • Error types and full stack traces
  • Custom source identifiers

Advanced Usage

Custom API URL or Proxy

If you need to point to a specific AnoSys instance:

from AnosysLoggers import setup_api

setup_api(path="https://custom.anosys.endpoint/api/log")

Troubleshooting

  • Missing API Key: Ensure ANOSYS_API_KEY is set in your environment.
  • Import Error: Ensure you installed anosys-logger-4-openai-agents.
  • Async Errors: Ensure you are await-ing async functions decorated with @anosys_logger.

License

MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anosys_logger_4_openai_agents-0.0.72.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anosys_logger_4_openai_agents-0.0.72-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file anosys_logger_4_openai_agents-0.0.72.tar.gz.

File metadata

File hashes

Hashes for anosys_logger_4_openai_agents-0.0.72.tar.gz
Algorithm Hash digest
SHA256 2bb9869897a193281da1f40c39f78301dad0c686187b9f00de1d608e5a6a1124
MD5 723086d2dd0db6acff2cef3e681db35b
BLAKE2b-256 d7d2492603f76a2439a3c920d695af49f26ff571abc62aaa4e32e7d45bf2c492

See more details on using hashes here.

Provenance

The following attestation bundles were made for anosys_logger_4_openai_agents-0.0.72.tar.gz:

Publisher: python-publish.yml on moisisv/anosys-logger-4-openai-agents

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file anosys_logger_4_openai_agents-0.0.72-py3-none-any.whl.

File metadata

File hashes

Hashes for anosys_logger_4_openai_agents-0.0.72-py3-none-any.whl
Algorithm Hash digest
SHA256 d51ed84aa49e33ab993c3f0fe4da14d248a03ed4c5804c074c0d9a5f43539d97
MD5 8057a88c58202e01808bdc15bfeb5b01
BLAKE2b-256 12ffb6943737b79cf3e0e658a64c51873aaa47fee99116b21201520577f6ae4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for anosys_logger_4_openai_agents-0.0.72-py3-none-any.whl:

Publisher: python-publish.yml on moisisv/anosys-logger-4-openai-agents

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page