Skip to content

Agent layer#23053

Merged
luisorofino merged 5 commits intoloa/openmetrics-ai-genfrom
loa/agent-layer
Mar 27, 2026
Merged

Agent layer#23053
luisorofino merged 5 commits intoloa/openmetrics-ai-genfrom
loa/agent-layer

Conversation

@luisorofino
Copy link
Copy Markdown

@luisorofino luisorofino commented Mar 25, 2026

What does this PR do?

Introduces the AnthropicAgent class, the core agent layer for the ddev AI tooling. This includes:

  • AnthropicAgent (ddev/src/ddev/ai/agent/client.py): An async agent that wraps the Anthropic API. It maintains multi-turn conversation history, supports tool filtering via an allowed_tools allowlist, maps API exceptions to typed error classes, and exposes a clean send() interface that returns a structured AgentResponse. Also, the following types are defined in this file: StopReason (StrEnum), ToolCall, TokenUsage and AgentResponse.

  • Exception wrapper (ddev/src/ddev/ai/agent/exceptions.py): Defines a hierarchy of AgentError subclasses (AgentConnectionError, AgentRateLimitError, AgentAPIError) that map directly to Anthropic SDK exception types.

  • Unit tests in ddev/tests/ai/agent/test_client.py.

Motivation

This is the third step in building the ddev AI agent framework. The previous PRs established the tool registry and tool framework; this PR adds the agent layer that drives conversations with the model and dispatches tool calls.

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add the qa/skip-qa label if the PR doesn't need to be tested during QA.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

@luisorofino luisorofino added the qa/skip-qa Automatically skip this PR for the next QA label Mar 25, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 25, 2026

Codecov Report

❌ Patch coverage is 98.68709% with 6 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (loa/openmetrics-ai-gen@b1882bc). Learn more about missing BASE report.

Additional details and impacted files
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@luisorofino luisorofino changed the title Implement AnthropicAgent, defined types and created tests Agent layer Mar 25, 2026
Copy link
Copy Markdown
Contributor

@AAraKKe AAraKKe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks looks very good! Just some comments to follow up on.

@AAraKKe AAraKKe marked this pull request as ready for review March 26, 2026 16:31
@AAraKKe AAraKKe requested a review from a team as a code owner March 26, 2026 16:31
Copy link
Copy Markdown
Contributor

@AAraKKe AAraKKe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed in a meeting and all comments have been applied. Merge when CI passes. Thanks!

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 80fb3d2150

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +140 to +141
if not self._programmatic_tool_calling:
definitions = [{**d, "allowed_callers": ALLOWED_TOOL_CALLERS} for d in definitions]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve direct tool invocation when PTC is disabled

AnthropicAgent._get_tool_definitions() currently adds "allowed_callers": ["code_execution_20260120"] when programmatic_tool_calling is False. In Anthropic tool semantics, omitting allowed_callers keeps tools model-invokable (direct), while restricting to code_execution_* opts the tool into programmatic/code-execution callers only. With the default constructor value (False), this effectively disables normal model tool calls in send() and makes tools unavailable in standard messages.create flows.

Useful? React with 👍 / 👎.

output_tokens=response.usage.output_tokens,
cache_read_input_tokens=cache_read,
cache_creation_input_tokens=cache_creation,
context=ContextUsage(window_size=await self._get_context_window(), used_tokens=used_tokens),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid dropping completed replies on context lookup failure

After messages.create() succeeds, send() unconditionally awaits _get_context_window() while building usage metadata. If models.retrieve() fails (network/API error), the method raises before history is updated and before returning the already-generated assistant output. This turns a successful completion into an exception path and can force retries (and duplicate spend) for transient metadata failures.

Useful? React with 👍 / 👎.

@luisorofino luisorofino merged commit 46ceb9b into loa/openmetrics-ai-gen Mar 27, 2026
314 of 316 checks passed
@luisorofino luisorofino deleted the loa/agent-layer branch March 27, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ddev qa/skip-qa Automatically skip this PR for the next QA team/agent-integrations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants