Skip to content

Commit c05c437

Browse files
committed
feat: add agent source tracking to user-agent header
1 parent 13e25e7 commit c05c437

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

runpod/user_agent.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ def construct_user_agent():
2525
if integration_method:
2626
ua_components.append(f"Integration/{integration_method}")
2727

28+
if os.getenv("CLAUDECODE") == "1":
29+
ua_components.append("(via claude-code)")
30+
2831
user_agent = " ".join(ua_components)
2932
return user_agent
3033

tests/test_user_agent.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,36 @@ def test_user_agent_with_integration(
5151
assert mock_system.called
5252

5353

54+
@patch("runpod.user_agent.platform.system", return_value="Linux")
55+
@patch("runpod.user_agent.platform.release", return_value="5.4")
56+
@patch("runpod.user_agent.platform.machine", return_value="x86_64")
57+
@patch("runpod.user_agent.platform.python_version", return_value="3.9.5")
58+
@patch.dict(os.environ, {"CLAUDECODE": "1"}, clear=False)
59+
def test_user_agent_with_claude_code(
60+
self, mock_python_version, mock_machine, mock_release, mock_system
61+
):
62+
"""Test the User-Agent string includes claude-code agent tag."""
63+
if "RUNPOD_UA_INTEGRATION" in os.environ:
64+
del os.environ["RUNPOD_UA_INTEGRATION"]
65+
66+
expected_ua = f"RunPod-Python-SDK/{runpod_version} (Linux 5.4; x86_64) Language/Python 3.9.5 (via claude-code)"
67+
self.assertEqual(construct_user_agent(), expected_ua)
68+
69+
@patch("runpod.user_agent.platform.system", return_value="Linux")
70+
@patch("runpod.user_agent.platform.release", return_value="5.4")
71+
@patch("runpod.user_agent.platform.machine", return_value="x86_64")
72+
@patch("runpod.user_agent.platform.python_version", return_value="3.9.5")
73+
def test_user_agent_without_claude_code(
74+
self, mock_python_version, mock_machine, mock_release, mock_system
75+
):
76+
"""Test the User-Agent string excludes agent tag when env var is not set."""
77+
for key in ("RUNPOD_UA_INTEGRATION", "CLAUDECODE"):
78+
if key in os.environ:
79+
del os.environ[key]
80+
81+
ua = construct_user_agent()
82+
self.assertNotIn("via claude-code", ua)
83+
84+
5485
if __name__ == "__main__":
5586
unittest.main()

0 commit comments

Comments
 (0)