atomic-agents
atomic-agents uses Instructor (which wraps the OpenAI client), so it supports custom endpoints by passing a configured client.
Install
pip install atomic-agents openai instructorConfigure
import instructor
from openai import OpenAI
from atomic_agents import AtomicAgent, AgentConfig
client = instructor.from_openai(
OpenAI(
base_url="https://api.doubleword.ai/v1",
api_key="{{apiKey}}",
)
)
agent = AtomicAgent(
config=AgentConfig(
client=client,
model="{{selectedModel.id}}",
)
)
response = agent.run(agent.input_schema(chat_message="Say hello."))
print(response.chat_message)The base_url and api_key are standard openai.OpenAI constructor parameters. instructor.from_openai() wraps the client for structured output support.
Prompt caching
AtomicAgent builds its system prompt as a string. One subclass turns it into a marked block:
import instructor
from openai import OpenAI
from atomic_agents import AtomicAgent, AgentConfig
client = instructor.from_openai(
OpenAI(
base_url="https://api.doubleword.ai/v1",
api_key="{{apiKey}}",
)
)
class CachedAgent(AtomicAgent):
def _build_system_messages(self):
messages = super()._build_system_messages()
if messages:
messages[0]["content"] = [{
"type": "text",
"text": messages[0]["content"],
"cache_control": {"type": "ephemeral", "ttl": "1h"},
}]
return messages
agent = CachedAgent(
config=AgentConfig(
client=client,
model="{{selectedModel.id}}",
system_prompt_generator=large_system_prompt,
)
)
agent.register_hook("completion:response", lambda r: print(r.usage))
response = agent.run(agent.input_schema(chat_message="What is 2 + 2?"))large_system_prompt is your SystemPromptGenerator and needs to clear the ~1024-token floor. Keep the default Mode.TOOLS because instructor's JSON modes rewrite the first content block. Read cache counts in the completion:response hook since instructor drops them from response.usage. See the prompt caching guide.