> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Other framework examples for agent optimization

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic has code examples

## Claude Agent SDK

<Accordion title="Click to expand the Claude Agent SDK example">
  Here is a complete example using the Claude Agent SDK:

  <CodeGroup>
    ```python title="Python" expandable lines wrap theme={null}
    from ldai import LDAIClient
    from ldai_optimization import (
        OptimizationResponse,
        LLMCallConfig,
        LLMCallContext,
        OptimizationClient,
        OptimizationFromConfigOptions
    )

    from ldai.tracker import TokenUsage

    from claude_agent_sdk import query, ClaudeAgentOptions
    from claude_agent_sdk.types import ResultMessage

    async def run_claude_optimization(optimization_key: str, ld_ai_client: LDAIClient):
        async def handle_agent_call(
            key: str,
            config: LLMCallConfig,
            context: LLMCallContext,
            is_evaluation: bool = False,
        ) -> OptimizationResponse:
            model = config.model.name if config.model else "claude-opus-4-5-20251101"
            final_message = None
            async for message in query(
                prompt=context.user_input or "",
                options=ClaudeAgentOptions(
                    system_prompt=config.instructions or "",
                    model=model,
                ),
            ):
                final_message = message

            if not isinstance(final_message, ResultMessage):
                raise ValueError(f"Unexpected final message type: {type(final_message)}")

            u = final_message.usage or {}
            input_tokens = u.get("input_tokens", 0)
            output_tokens = u.get("output_tokens", 0)

            return OptimizationResponse(
                output=final_message.result or "",
                usage=TokenUsage(
                    total=input_tokens + output_tokens,
                    input=input_tokens,
                    output=output_tokens,
                ),
            )

        options = OptimizationFromConfigOptions(
            project_key="default",
            handle_agent_call=handle_agent_call,
            handle_judge_call=handle_agent_call,
        )

        client = OptimizationClient(ld_ai_client)
        result = await client.optimize_from_config(optimization_key, options)

        return result
    ```
  </CodeGroup>
</Accordion>

## OpenAI Agents SDK

<Accordion title="Click to expand the OpenAI Agents SDK example">
  Here is a complete example using the OpenAI Agents SDK:

  <CodeGroup>
    ```python title="Python" expandable lines wrap theme={null}
    from ldai import LDAIClient
    from ldai_optimization import (
        OptimizationResponse,
        LLMCallConfig,
        LLMCallContext,
        OptimizationClient,
        OptimizationFromConfigOptions
    )

    from agents import Agent
    from agents.run import Runner

    from ldai.tracker import TokenUsage

    async def run_openai_optimization(optimization_key: str, ld_ai_client: LDAIClient):
        async def handle_agent_call(
            key: str,
            config: LLMCallConfig,
            context: LLMCallContext,
            is_evaluation: bool = False,
        ) -> OptimizationResponse:
            model = config.model.get_parameter("name") if config.model else "gpt-5"
            root = Agent(
                name=key,
                instructions=config.instructions,
                handoffs=[],
                tools=[],
                model=model,
            )
            response = await Runner.run(root, context.user_input or "")
            u = response.context_wrapper.usage
            return OptimizationResponse(
                output=response.final_output,
                usage=TokenUsage(
                    total=u.total_tokens, input=u.input_tokens, output=u.output_tokens
                ),
            )

        client = OptimizationClient(ld_ai_client)

        options = OptimizationFromConfigOptions(
            project_key="default",
            handle_agent_call=handle_agent_call,
            handle_judge_call=handle_agent_call,
        )

        result = await client.optimize_from_config(optimization_key, options)

        return result
    ```
  </CodeGroup>
</Accordion>

## LangChain `create_agent`

<Accordion title="Click to expand the LangChain `create_agent` example">
  Here is a complete example using LangChain's `create_agent`:

  <CodeGroup>
    ```python title="Python" expandable lines wrap theme={null}
    from ldai_optimization import (
        OptimizationResponse,
        LLMCallConfig,
        LLMCallContext,
        OptimizationClient,
        OptimizationFromConfigOptions
    )

    from ldai.tracker import TokenUsage

    from ldai import LDAIClient

    from langchain.agents import create_agent
    from langchain.messages import HumanMessage

    async def run_langgraph_optimization(optimization_key: str, ld_ai_client: LDAIClient):
        async def handle_agent_call(
            key: str,
            config: LLMCallConfig,
            context: LLMCallContext,
            is_evaluation: bool = False,
        ) -> OptimizationResponse:
            model = config.model.get_parameter("name") if config.model else "openai:gpt-5"

            agent = create_agent(
                model=model,
                system_prompt=config.instructions,
            )

            response = agent.invoke(
                { "messages" : [HumanMessage(context.user_input or "Complete the request")] }
            )

            last_message = response['messages'][-1]
            u = last_message.usage_metadata
            return OptimizationResponse(
                output=last_message.content,
                usage=TokenUsage(
                    total=u["total_tokens"], input=u["input_tokens"], output=u["output_tokens"]
                ),
            )

        options = OptimizationFromConfigOptions(
            project_key="default",
            handle_agent_call=handle_agent_call,
            handle_judge_call=handle_agent_call,
        )

        client = OptimizationClient(ld_ai_client)
        result = await client.optimize_from_config(optimization_key, options)

        return result
    ```
  </CodeGroup>
</Accordion>

## Strands

<Accordion title="Click to expand the Strands example">
  Here is a complete example using Strands:

  <CodeGroup>
    ```python title="Python" expandable lines wrap theme={null}
    from ldai_optimization import (
        OptimizationResponse,
        LLMCallConfig,
        LLMCallContext,
        OptimizationClient,
        OptimizationFromConfigOptions
    )

    from ldai.tracker import TokenUsage

    from ldai import LDAIClient

    from strands import Agent
    from strands.models.openai import OpenAIModel

    async def run_strands_optimization(optimization_key: str, ld_ai_client: LDAIClient):
        async def handle_agent_call(
            key: str,
            config: LLMCallConfig,
            context: LLMCallContext,
            is_evaluation: bool = False,
        ) -> OptimizationResponse:
            model = config.model.get_parameter("name") if config.model else "gpt-5"
            params = config.model.get_parameter("params") if config.model else {}

            openai_connector = OpenAIModel(
                model_id=model,
                params=params if params else {}
            )

            agent = Agent(system_prompt=config.instructions, model=openai_connector, callback_handler=None)

            response = agent(context.user_input)

            u = response.metrics.get_summary()["accumulated_usage"]

            return OptimizationResponse(
                output=str(response),
                usage=TokenUsage(
                    total=u["totalTokens"], input=u["inputTokens"], output=u["outputTokens"]
                ),
            )

        options = OptimizationFromConfigOptions(
            project_key="default",
            handle_agent_call=handle_agent_call,
            handle_judge_call=handle_agent_call,
        )

        client = OptimizationClient(ld_ai_client)
        result = await client.optimize_from_config(optimization_key, options)

        return result
    ```
  </CodeGroup>
</Accordion>
