AgentOps-AI / agentops

Python SDK for agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks like CrewAI, Langchain, and Autogen
https://agentops.ai
MIT License
1.18k stars 93 forks source link

Return cost in dollars after end_session #249

Open charlesmaddock opened 3 weeks ago

charlesmaddock commented 3 weeks ago

šŸš€ Feature Request

I found it convenient to return the cost after end session so I can display it to my users like so:

cost = agentops.end_session("Success")

I solved this problem by returning the cost a few times:

# agentops/__init__.py
def end_session(
    end_state: str,
    end_state_reason: Optional[str] = None,
    video: Optional[str] = None,
    is_auto_end: Optional[bool] = False,
):
    """
    End the current session with the AgentOps service.

    Args:
        end_state (str): The final state of the session. Options: Success, Fail, or Indeterminate.
        end_state_reason (str, optional): The reason for ending the session.
        video (str, optional): URL to a video recording of the session
        is_auto_end (bool, optional): is this an automatic use of end_session and should be skipped with bypass_auto_end_session
    """
    return Client().end_session(
        end_state=end_state,
        end_state_reason=end_state_reason,
        video=video,
        is_auto_end=is_auto_end,
    )

Inside client.py the cost is printed when the session is ended, so I just grabbed it from there.

# client.py
            if self._worker is None or self._worker._session is None:
            return logger.warning("Cannot end session - no current worker or session")

        self._session.video = video
        self._session.end_session(end_state, end_state_reason)
        token_cost = self._worker.end_session(self._session)

        if token_cost == "unknown":
            logger.info("Could not determine cost of run.")
        else:
            token_cost_d = Decimal(token_cost)
            logger.info(
                "This run's cost ${}".format(
                    "{:.2f}".format(token_cost_d)
                    if token_cost_d == 0
                    else "{:.6f}".format(token_cost_d)
                )
            )

        logger.info(
            colored(
                f"\x1b[34mSession Replay: https://app.agentops.ai/drilldown?session_id={self._session.session_id}\x1b[0m",
                "blue",
            )
        )

        self._session = None
        self._worker = None
        return token_cost # return cost here since it is printed anyways.