khoj-ai/khoj · error · ValidationError
A private Agent with the name {instance.name} already exists
Error message
A private Agent with the name {instance.name} already exists. What it means
Django pre_save signal rejecting creation of a new Agent whose name collides with an agent created by the same creator. Enforces per-creator name uniqueness for private agents.
Source
Thrown at src/khoj/database/models/__init__.py:372
SCHEDULED_JOB = "scheduled_job"
SCHEDULE_LEADER = "schedule_leader"
APPLY_MIGRATIONS = "apply_migrations"
# We need to make sure that some operations are thread-safe. To do so, add locks for potentially shared operations.
# For example, we need to make sure that only one process is updating the embeddings at a time.
name = models.CharField(max_length=200, choices=Operation.choices, unique=True)
started_at = models.DateTimeField(auto_now_add=True)
max_duration_in_seconds = models.IntegerField(default=60 * 60 * 12) # 12 hours
@receiver(pre_save, sender=Agent)
def verify_agent(sender, instance, **kwargs):
# check if this is a new instance
if instance._state.adding:
if Agent.objects.filter(name=instance.name, privacy_level=Agent.PrivacyLevel.PUBLIC).exists():
raise ValidationError(f"A public Agent with the name {instance.name} already exists.")
if Agent.objects.filter(name=instance.name, creator=instance.creator).exists():
raise ValidationError(f"A private Agent with the name {instance.name} already exists.")
class NotionConfig(DbBaseModel):
token = models.CharField(max_length=200)
user = models.ForeignKey(KhojUser, on_delete=models.CASCADE)
class GithubConfig(DbBaseModel):
pat_token = models.CharField(max_length=200)
user = models.ForeignKey(KhojUser, on_delete=models.CASCADE)
class GithubRepoConfig(DbBaseModel):
name = models.CharField(max_length=200)
owner = models.CharField(max_length=200)
branch = models.CharField(max_length=200)
github_config = models.ForeignKey(GithubConfig, on_delete=models.CASCADE, related_name="githubrepoconfig")
View on GitHub (pinned to ae229ca894)
Solutions
- Rename the new agent to something the user hasn't used.
- Return the existing agent instead of creating a duplicate (upsert semantics).
- Debounce/disable the create button after first submit to avoid double POSTs.
Example fix
# before
Agent.objects.create(name="MyBot", creator=user, privacy_level=Agent.PrivacyLevel.PRIVATE)
# after
agent, created = Agent.objects.get_or_create(
name="MyBot", creator=user,
defaults={"privacy_level": Agent.PrivacyLevel.PRIVATE},
) Defensive patterns
Strategy: validation
Validate before calling
from khoj.database.models import Agent
agent, created = Agent.objects.get_or_create(
name=name, creator=user,
defaults={"privacy_level": Agent.PrivacyLevel.PRIVATE},
) Try / catch
from django.core.exceptions import ValidationError
try:
agent.save()
except ValidationError as e:
if "private Agent" in str(e):
agent = Agent.objects.get(name=agent.name, creator=agent.creator)
else:
raise Prevention
- Enforce per-user name uniqueness in the form/serializer with a friendly message.
- Use get_or_create or update-or-create semantics for idempotent writes.
When it happens
Trigger: A user creates a second agent with a name they already used before (any privacy level), e.g. repeated POST to the agent-creation API with the same name.
Common situations: Double-submitted forms; retry of a failed request that actually succeeded; seed scripts re-run for the same user.
Related errors
- A public Agent with the name {instance.name} already exists.
- Invalid conversation_log format: {str(e)}
- An Entry cannot be associated with both a user and an agent.
- Invalid conversation settings. Configure some chat model on
- Invalid automation id: {automation_id}
AI-assisted analysis of khoj-ai/khoj@ae229ca894 (2026-08-27).
Data as JSON: /api/errors/1db1263bd4259699.
Report an issue: GitHub.