Skip to content

Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI

Langchain callback

Braelyn Boynton4f5a6da315f0
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 00000000..13566b81
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/agentops.iml b/.idea/agentops.iml
new file mode 100644
index 00000000..51951249
--- /dev/null
+++ b/.idea/agentops.iml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+ <component name="NewModuleRootManager">
+ <content url="file://$MODULE_DIR$">
+ <excludeFolder url="file://$MODULE_DIR$/venv" />
+ </content>
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ </component>
+ <component name="PyDocumentationSettings">
+ <option name="format" value="PLAIN" />
+ <option name="myDocStringFormat" value="Plain" />
+ </component>
+ <component name="TestRunnerService">
+ <option name="PROJECT_TEST_RUNNER" value="py.test" />
+ </component>
+</module>
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 00000000..105ce2da
--- /dev/null
+++ b/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+ <settings>
+ <option name="USE_PROJECT_PROFILE" value="false" />
+ <version value="1.0" />
+ </settings>
+</component>
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 00000000..35f08d37
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="Black">
+ <option name="sdkName" value="Python 3.9 (agentops)" />
+ </component>
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (agentops)" project-jdk-type="Python SDK" />
+</project>
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 00000000..4cf19044
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="ProjectModuleManager">
+ <modules>
+ <module fileurl="file://$PROJECT_DIR$/.idea/agentops.iml" filepath="$PROJECT_DIR$/.idea/agentops.iml" />
+ </modules>
+ </component>
+</project>
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 00000000..35eb1ddf
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="VcsDirectoryMappings">
+ <mapping directory="" vcs="Git" />
+ </component>
+</project>
\ No newline at end of file
diff --git a/agentops/langchain_callback_handler.py b/agentops/langchain_callback_handler.py
new file mode 100644
index 00000000..cf132bfd
--- /dev/null
+++ b/agentops/langchain_callback_handler.py
@@ -0,0 +1,51 @@
+from typing import Dict, Any, List, Optional
+from uuid import UUID
+
+from langchain_core.outputs import LLMResult
+
+from agentops import Client as AOClient
+from agentops import Event
+
+from langchain.callbacks.base import BaseCallbackHandler
+
+from agentops.helpers import get_ISO_time
+
+
+class LangchainCallbackHandler(BaseCallbackHandler):
+
+ def __init__(self):
+ self.ao_client = AOClient()
+ self.ao_client.start_session()
+
+ # keypair <run_id: str, Event>
+ self.events = {}
+
+ def on_llm_start(
+ self,
+ serialized: Dict[str, Any],
+ prompts: List[str],
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ tags: Optional[List[str]] = None,
+ metadata: Optional[Dict[str, Any]] = None,
+ **kwargs: Any,
+ ) -> Any:
+ self.events[run_id] = Event(
+ event_type="langchain_llm",
+ tags=tags,
+ prompt="\n--\n".join(prompts),
+ init_timestamp=get_ISO_time()
+ )
+
+ def on_llm_end(
+ self,
+ response: LLMResult,
+ *,
+ run_id: UUID,
+ parent_run_id: Optional[UUID] = None,
+ **kwargs: Any,
+ ) -> Any:
+ self.events[run_id].end_timestamp = get_ISO_time()
+ self.ao_client.record(self.events[run_id])
+
diff --git a/requirements.txt b/requirements.txt
index 45f6e9f6..0d62c32f 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,5 @@
pytest==7.4.0
requests==2.31.0
requests-mock==1.11.0
-pydantic==2.4.2
\ No newline at end of file
+pydantic==2.4.2
+langchain==0.0.350
\ No newline at end of file